Mega Code Archive

 
Categories / Delphi / Ide Indy
 

How do I write the client side Delphi code to send a request and process the returned data

Title: How do I write the client side Delphi code to send a request and process the returned data? Let's recap. Your Delphi code has to send a request to the web server to ask for a list of people having a birthday on a specific date. Your code has to then process the returned data and display it on a form. For the purposes of this exercise we will put the list of names in a StringList. So the request that we wish to send to the server is something like: http://www.towerbase.34sp.com/birthdays.php?day=24&month=07 How do we send that to a web server? It is surprisingly easy if you have the Indy components (supplied with Delphi 6 and 7). The hardest part is finding the right Indy component because there are dozens of them! The component you want is TIdHTTP which is on the Indy Clients tab. It's icon is a blue globe and is 11th from the left in my IDE. Drop this on your form. Set the name property to HTTP. Drop a TButton on the form and in the OnClick handler for the button code the following: CODE const URL = 'http://www.towerbase.34sp.com/birthdays.php?day=%d&month=%d'; var command: string; response: string; begin command := Format ( URL, [ DayOf(Now), MonthOf(Now) ] ); response := HTTP.Get ( command ); ExtractNames ( response ); end; To keep things simple, I've assumed you want a list of people whose birthday is today. But you could make it more flexible by dropping a Calender on the form and using that to select a date. The response variable will contain the entire HTML for the web page. In particular it will contain the names we want embedded within paragraph tags. The ExtractNames routine would extract the names from within these tags and add them to a string list. This task is made particularly easy because each starts on a new line, followed by a line containing the name followed by a line containing the . If you want to try this out then you can use the URL supplied above.