TFPHttpClient.SimpleGet exception 301 lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- winni · 2022-03-10 18:26 Hi! After 15 years of disruption I try to dive again in the  world of http and html. My  first test was taken from: https://wiki.freepascal.org/fphttpclient Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i writeln(TFPHttpClient.SimpleGet('https://freepascal.org')); This results in an exception EHTTPclient 301 If 301 is the httpError it means "Moved Permanently" Can somebody give me a hint how to get around that? And the example in the wiki should be changed. Winni ---------------------------------------------------------------------- korba812 · 2022-03-10 18:38 I'm not sure but the ssl/tls handler is probably missing. Read the "HTTPS (TLS / SSL)" section in the specified wiki page. ---------------------------------------------------------------------- winni · 2022-03-10 18:53 Hi! No - that is not the problem. This works with no problems: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i memo1.Lines.Text:= TFPHttpClient.SimpleGet('https://www.spiegel.de'); That gives me the Spiegel magazine in HTML. The question is: How to react on http error messages? Winni ---------------------------------------------------------------------- korba812 · 2022-03-10 19:09 Ah... 301... You get the address In response, which you should call in the next request. SimpleGet likely does not follow the redirect. ---------------------------------------------------------------------- Remy Lebeau · 2022-03-10 21:34 Quote from: winni on March 10, 2022, 07:26:34 pmThis results in an exception EHTTPclient 301 If 301 is the httpError it means "Moved Permanently" Can somebody give me a hint how to get around that? 301 is an HTTP redirect.  The response includes a Location header specifying a new URL that the client needs to repeat the request to.  However, SimpleGet() does not follow redirects automatically.  This is even explained in the same documentation that you had linked to: QuoteBut there are some caveats with this code. The foremost one being that this code does not allow for redirects. Let me demo that: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program dl_fphttp_redirect_error;{$mode delphi}{$ifdef windows}{$apptype console}{$endif}uses   sysutils, fphttpclient, opensslsockets;begin try    writeln(TFPHttpClient.SimpleGet('https://google.com'));   except on E:EHttpClient do    writeln(e.message) else raise;  end;end. Because the Google URL uses redirects, there is an exception: "Unexpected response status code: 301". In such a case we need more control, which I will show you in the next example: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program dl_fphttp_c; {$mode delphi}{$ifdef windows}{$apptype console}{$endif} uses  classes,   fphttpclient,  openssl, { This implements the procedure InitSSLInterface }  opensslsockets; var  Client: TFPHttpClient; begin   { SSL initialization has to be done by hand here }  InitSSLInterface;   Client := TFPHttpClient.Create(nil);  try    { Allow redirections }    Client.AllowRedirect := true;    writeln(Client.Get('https://google.com/'));   finally    Client.Free;  end;end. You see this requires slightly more code, but it is still a very small program. Note the addition of the AllowRedirect property assignment. Quote from: winni on March 10, 2022, 07:26:34 pmAnd the example in the wiki should be changed. No, it shouldn't, because there is nothing wrong with it.  You just didn't read the rest of the page carefully enough. ---------------------------------------------------------------------- winni · 2022-03-10 21:58 Thanx!!!