Search code examples
htmldelphidelphi-xe3

Load HTML source code


How do I load HTML source code? I tried a few functions with no luck. This one works for me on many sites, but not all of them:

function LoadWebPageToString(MyUrl: String): String;  //load HTML content from a webpage to a string
begin
  Result := 'Error';
  with TIdHTTP.Create do
  begin
    try
      Result := Get(MyUrl);
    finally
      Free;
    end;
  end;
end;

When it fails, I get this error:

HTTP/1.1 403 forbidden

The target page is just a normal page. It loads normally via HTTP, doesn't require (think nor supports) HTTPS. Maybe it is about cookies or something? I don't know.


Solution

  • One of the reasons of the HTTP/1.1 403 forbidden is which the server doesn't recognizes the user agent of the client, so try setting the useragent property like so .

    Request.UserAgent:='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0';