We help IT Professionals succeed at work.

Capturing a website from Delphi

sheepfarmer
sheepfarmer asked
on
Medium Priority
590 Views
Last Modified: 2013-11-23
Is it a fairly simple process to request a website from within a Delphi application and have the resultant webpage or resultant data returned and stored in a variable for further  processing.

e.g.
Send the following
 GET http://www.mywebsite.com/status

Receive the following into a variable
<html><head><title>System Status</title></head><body>Status OK</body></html>

Ideally, it would also be good to capture the header info (e.g. Error 404 or similar)

Thanks
SF
Comment
Watch Question

Commented:
Hi,
You can use the Indy components that comes with delphi to do that.
Try the code I've attached below, add the units in the uses clause, and then add a button and a memo to the form, and connect the button1Click event to the button.

I don't know what Delphi version you are using, but the example below is made for Delphi6. I know that the Indy components have changed somewhat over the delphi versions, but the functionality will be somewhat like what you see below.

regards
Hypo
uses ..., IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP;
 
procedure TForm1.Button1Click(Sender: TObject);
var IdHTTPClient : TIdHTTP;
begin
  IdHTTPClient := TIdHTTP.Create(nil);
  try
    Memo1.Text := IdHTTPClient.Get('http://www.google.se');
  finally
    IdHTTPClient.Free;
  end;
end;

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Top banana.  Thanks for this.
Do the Indy HTTP components allow for such things analysis of the returned page, headers etc?
Commented:
Sure,
the Indy components have quite alot of good functionality for handeling network communication. You can access the latest headerresponse by reading the IdHTTPClient.Response property. You also have access to the reply code by reading the property IdHTTPClient.ResponseCode.

When using the Indy components, you should be aware that they often throw exceptions when unexpected situations occur. A common example is when you use the TIdTCPServer component, which throws an EConnectionClosedGracefully exception when the client closes a connection before the server does. So It is a good thing to catch such exceptions with try-except blocks, and also to remember that NOT all raised exceptions indicate errors in your code. :)

regards
Hypo

Author

Commented:
Thanks for all that - much apprecaited.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.