This exercise may be simple... but it isn't a bad place to get started. AND it can be developed into a wealth of useful tools. I wrote it along the road to providing myself with a way to see how the different pages at my website link to one another.
In this tutorial, you'll be able to check that you have Synapse installed, to support your Lazarus (and, I believe, Delphi) programming.
We're going to create a tiny application. The ser gives it the URL of a page on the World Wide Web, and the application goes off, looks at the page, and fetches back the HTML behind that page, and displays it in a memo.
Every web-browser I ever encountered offers something similar, of course... so why write your own?
Once you know how to get the web page's HTML into a memo, you have taken care of the biggest thing between you and a program to scan multiple pages for whatever you want. And that's just for starters.
The work will be done with Lazarus and the free IP/TPC library called Synapse. I have a page about why I like Synapse, if you haven't made your choice yet.
In any case, the application would serve as a quick test of your Synapse installation. (I've done notes on installing Synapse, so that your Lazarus installation has access to the tools Synapse provides.)
Start a new application... The "blank" project loaded when you tell Lazarus "File/ New/ Project/ Application" will suffice.
Name your form. I called my project LDN183 (Lazarus Demo, New series), and the main form LDN183f1. You'll see that, and variants in what follows. Either use the same name, of make adjustments, as necessary.
Save what you've got so far. I called the unit LDN183u1, and the project LDN183.
Add a button to the form, call it buQuit, create an OnClick handler which does application.terminate, or close.
Make sure that much works!
Put a memo on the page. Call it meDisplayPage. Set the Scrollbars property to AutoBoth.
In the app's code, add...
... to the Uses clause
.. and run the application. The httpsend won't "do" anything yet... but if your setup isn't right yet, the compiler will complain. Might as well get that working now, fix one thing at a time.
Copy the following into the shell- of- a- program you've built so far. Put it just before the "end." (...the "end" at the end of the code!)
function DownloadHTTP(URL, TargetFile: string): Boolean; //From http://wiki.lazarus.freepascal.org/Synapse //Accessed 23 Oct 17 var HTTPGetResult: Boolean; HTTPSender: THTTPSend; begin Result := False; HTTPSender := THTTPSend.Create; try HTTPGetResult := HTTPSender.HTTPMethod('GET', URL); if (HTTPSender.ResultCode >= 100) and (HTTPSender.ResultCode<=299) then begin HTTPSender.Document.SaveToFile(TargetFile); Result := True; end; finally HTTPSender.Free; end; end;
... and put...
function DownloadHTTP(URL, TargetFile: string): Boolean;
... in one of the declarations sections of the class definition further up the page. I used the "private" section. The "public" would probably "do", too.
Go back to the long thing you inserted a moment ago, and add "TLDN183f1." in front of the "DownloadHTTP(URL, TargetFile...)" (Assuming you called your application LDN183, and the form LDN183f1, of course. Adapt as necessary, if you didn't.)
Now we have a framework in place.
Time to start exercising it...
Add a memo, call it meDisplayHTML
Add an edit box, call it eURL. Use the Object inspector to make eURL.text "http://SheepdogGuides.com" (no quotes)
Add a button. Call it buFetch, make caption "Fetch Page".
Make it's OnClick handler...
procedure TLDN183f1.buFetchClick(Sender: TObject); var boTmp:boolean; begin boTmp:=DownloadHTTP(eURL.text, 'TmpPageFetch.htm'); end;
... and run the program. After you have run it, look in the folder your .exe is in. You should find a file called TmpPageFetch.htm, which wasn't there before. Double click, and it should open in your browser, being a local copy of what is out there on the internet.
Okay... not QUITE what I wanted... but a starting point.
I wanted the source of the page loaded to meDisplayPage.
But first... How did I know how to put the call of DownloadHTTP?
I didn't! but looking at the header...
DownloadHTTP(URL, TargetFile: string): Boolean;
... gave me BIG hints, and with a little fumbling, I got there!
The following is "better", albeit a bit "round the houses"...
procedure TLDN183f1.buFetchClick(Sender: TObject); var boTmp:boolean; begin boTmp:=DownloadHTTP(eURL.text, 'TmpPageFetch.htm'); meDisplayPage.Lines.LoadFromFile('TmpPageFetch.htm'); end;
I suspect there's a way to use filestreams for this, and avoid the disk thrash... but after two hours of failing to find that solution, I "moved on". (If you can show me how to do this with filestream objects, I'd be grateful!)
I struggled for ages, trying to get DownloadHTTP to write to a TFilestream object.
Wasn't finding much in the way of web-chatter about DownloadHTTP...
And then discovered...
.. and two minutes later had...
THE PROGRAM ISN'T "good" in current state... It doesn't check for, deal with "problem" situations... but it works, and the "other stuff" can be built on top of this foundation!
But for what it is worth, here's what the final buFetchClick looked like. A bit simpler than the previous one, and it won't thrash your hard drive!
procedure TLDN183f1.buFetchClick(Sender: TObject); //For help, see http://synapse.ararat.cz/doc/help/httpsend.html#HttpGetText var boTmp:boolean; begin boTmp:=HTTPGetText(eURL.text,meDisplayPage.lines); end;
I hope you've found the information here useful? Facebook "likes", Google "pluses" make me happy, and they also help others who'd like to know this sort of thing FIND this page...
I lied. Sorry. You don't need the function DownloadHTTP if you want to "go with" the HTTPGetText solution. Sorry. But what you see above is the tale of my "journey of discovery". And maybe you want a way to save to file? By the time I'd got this far, my dinner was long overdue, and I decided that sparing you a quick copy/ paste wasn't a top priority. Anyway... we wouldn't have had the nice Filestreams discussion!
BEWARE: The search above only visits a selection of my stuff. Go to either of my other sites (see links below) and use that site's FreeFind search button, if you haven't found something you "know is there".
In addition to the tutorials for which this page serves as Table of Contents, I have other sites with material you might find useful.....
My other sites....
Sheepdog Software homepage.
My Arunet homepage.
... and some links to specific pages within them you might want....
You can't "play" all day... learn to use the Libre Office/ Open Office database. Free. Multi-platform.
The Arduino- LOTS of fun, for not much money. And beginner (intelligent beginner) friendly. And good pursuit for kids. Combine programming and electronics!
Designing printed circuit boards the KiCad way. Free. Multi-platform. Long established. PCB-fab houses take native KiCad files.
And lastly... Making maps... how we did it before GPS Indulge me? This discusses a worthwhile, fun (if presented intelligently) activity for kids, which can be undertaken on many levels... a simple 20 minutes, or weeks of engaging activity. (Also known to divert susceptible adults.)
Here is the way to contact the author of these Lazarus/Delphi tutorials, Tom Boyd.
Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. (Mostly passes. A few problems caused by Google+ and Delicious code.)
If this page causes a script to run, why? Because of things like Google panels, and the code for the search button. Why do I mention scripts? Be sure you know all you need to about spyware.
....... P a g e . . . E n d s .....