Recent

Author Topic: TFPHttpClient.SimpleGet exception 301  (Read 1552 times)

winni

  • Hero Member
  • *****
  • Posts: 3197
TFPHttpClient.SimpleGet exception 301
« on: March 10, 2022, 07:26:34 pm »
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][+][-]
  1. writeln(TFPHttpClient.SimpleGet('https://freepascal.org'));
  2.  


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

  • Sr. Member
  • ****
  • Posts: 392
Re: TFPHttpClient.SimpleGet exception 301
« Reply #1 on: March 10, 2022, 07:38:00 pm »
I'm not sure but the ssl/tls handler is probably missing. Read the "HTTPS (TLS / SSL)" section in the specified wiki page.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TFPHttpClient.SimpleGet exception 301
« Reply #2 on: March 10, 2022, 07:53:48 pm »
Hi!

No - that is not the problem.

This works with no problems:

Code: Pascal  [Select][+][-]
  1. 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

  • Sr. Member
  • ****
  • Posts: 392
Re: TFPHttpClient.SimpleGet exception 301
« Reply #3 on: March 10, 2022, 08:09:20 pm »
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

  • Hero Member
  • *****
  • Posts: 1312
    • Lebeau Software
Re: TFPHttpClient.SimpleGet exception 301
« Reply #4 on: March 10, 2022, 10:34:12 pm »
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:

Quote
But 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][+][-]
  1. program dl_fphttp_redirect_error;
  2. {$mode delphi}{$ifdef windows}{$apptype console}{$endif}
  3. uses
  4.   sysutils, fphttpclient, opensslsockets;
  5. begin  
  6.   try
  7.     writeln(TFPHttpClient.SimpleGet('https://google.com'));
  8.   except on E:EHttpClient do
  9.     writeln(e.message) else raise;
  10.   end;
  11. 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][+][-]
  1. program dl_fphttp_c;
  2.  
  3. {$mode delphi}{$ifdef windows}{$apptype console}{$endif}
  4.  
  5. uses
  6.   classes,
  7.   fphttpclient,
  8.   openssl, { This implements the procedure InitSSLInterface }
  9.   opensslsockets;
  10.  
  11. var
  12.   Client: TFPHttpClient;
  13.  
  14. begin
  15.  
  16.   { SSL initialization has to be done by hand here }
  17.   InitSSLInterface;
  18.  
  19.   Client := TFPHttpClient.Create(nil);
  20.   try
  21.     { Allow redirections }
  22.     Client.AllowRedirect := true;
  23.     writeln(Client.Get('https://google.com/'));
  24.   finally
  25.     Client.Free;
  26.   end;
  27. 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.
« Last Edit: March 10, 2022, 10:38:31 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TFPHttpClient.SimpleGet exception 301
« Reply #5 on: March 10, 2022, 10:58:03 pm »


Thanx!!!

 

TinyPortal © 2005-2018