22 Votes

Lazarus: Get HTML of a Website

Question by Guest | 2015-03-23 at 09:56

I would like to read out the HTML code of an Internet page using Lazarus (URL is given). It is not about displaying the HTML in a browser complement or something like that. I am only interested in the content because I need some information from a webpage for my application.

Is there any very simple light-weight method I can use? I do not want to include one of those huge Internet libraries into my program, because I really only need this function. I prefer a solution that is platform independent.

ReplyPositiveNegative
2Best Answer4 Votes

You can just use fphttpclient which is part of the fcl-web package and therefore automatically included in FreePascal and Lazarus without need to install additional components or libraries. You just have to add "fphttpclient" to the uses clause of your unit.

Here is a simple example:

uses fphttpclient;
  
procedure TForm1.Button1Click(Sender: TObject);
var
  url, html : String;
begin
  url := 'http://www.example.com';
  html := TFPCustomHTTPClient.SimpleGet(url);
  memo1.Text := html;
end;

In this example, we are picking the HTML code from example.com in order to show it within a memo. For this, we are using the class function SimpleGet of TFPCustomHTTPClient which allow us to do the job within only one line of code.

Of course, you can also go the detailed way:

uses fphttpclient;

procedure TForm1.Button2Click(Sender: TObject);
var
  httpclient: TFPHTTPClient;
  url, html : String;
begin

  url := 'http://www.example.com';

  httpclient := TFPHttpClient.Create(Nil);
  try
    html := httpclient.Get(url);
  finally
    httpclient.Free;
  end;

  memo1.Text := html;

end;   

In both cases, the result is the same. The second example is a little bit longer but you can use it as a basis if you would like to extend the code in some way.
2015-03-23 at 12:37

ReplyPositive Negative
Reply

Related Topics

PHP: Sending an E-Mail

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.