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?
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:
But there are some caveats with this code. The foremost one being that this code does not allow for redirects. Let me demo that:
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:
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.
And 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.