Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a web page into a variable?

Tags:

delphi

I don't care whether it is a string, stringlist, memo, etc ... but not a disk file

How do I download a compete web page into a variable? Thanks

like image 520
Mawg says reinstate Monica Avatar asked Feb 11 '12 09:02

Mawg says reinstate Monica


1 Answers

Using Indy:

uses IdHTTP;

const
  HTTP_RESPONSE_OK = 200;

function GetPage(aURL: string): string;
var
  Response: TStringStream;
  HTTP: TIdHTTP;
begin
  Result := '';
  Response := TStringStream.Create('');
  try
    HTTP := TIdHTTP.Create(nil);
    try
      HTTP.Get(aURL, Response);
      if HTTP.ResponseCode = HTTP_RESPONSE_OK then begin
        Result := Response.DataString;
      end else begin
        // TODO -cLogging: add some logging
      end;
    finally
      HTTP.Free;
    end;
  finally
    Response.Free;
  end;
end;
like image 182
Marjan Venema Avatar answered Oct 13 '22 22:10

Marjan Venema