Recent

Author Topic: SOLVED Download a file from a redirection web server with TFPHTTPClient  (Read 3438 times)

Segator

  • Full Member
  • ***
  • Posts: 168
    • https://github.com/Nenirey
Hi developers, i extract some code from the online package manager to make a simple general downloader using TFPHTTPClient, the program work ok when the URL is static or directly to the file but when the server redirect i can not download the file, i attech the example here, how i can deal with the server redirections?.
« Last Edit: November 14, 2018, 12:54:31 am by Segator »
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Download a file from a redirection web server with TFPHTTPClient
« Reply #1 on: November 10, 2018, 06:42:38 pm »
TFpHttpClient.AllowRedirects := true;
Specialize a type, not a var.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Download a file from a redirection web server with TFPHTTPClient
« Reply #2 on: November 11, 2018, 12:22:42 am »
Be careful, some servers implement redirects that violate the standard. fphttpclient only changes method to GET if the server returns 303, otherwise the original method is preserved for the redirected request. This might cause unexpected behavior if the server expects GET but returning 301/302 (technically changing to GET is valid for these two, but it's optional so fphttpclient doesn't do so) /307/308.

Segator

  • Full Member
  • ***
  • Posts: 168
    • https://github.com/Nenirey
Re: Download a file from a redirection web server with TFPHTTPClient
« Reply #3 on: November 11, 2018, 12:41:39 am »
TFpHttpClient.AllowRedirects := true;
Hi Thaddy thanks to response, its so simpe?
Be careful, some servers implement redirects that violate the standard. fphttpclient only changes method to GET if the server returns 303, otherwise the original method is preserved for the redirected request. This might cause unexpected behavior if the server expects GET but returning 301/302 (technically changing to GET is valid for these two, but it's optional so fphttpclient doesn't do so) /307/308.
but i can force the GET method for 301,302,307 and 308? how i can force to accept bad redirect standard?
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Download a file from a redirection web server with TFPHTTPClient
« Reply #4 on: November 11, 2018, 12:56:25 am »
but i can force the GET method for 301,302,307 and 308? how i can force to accept bad redirect standard?

Basically intercepting the redirection yourself: set AllowRedirects to False and examine ResponseStatusCode for a 30x response. Do note that this is mostly useful when your initial request is a POST; if it's a GET you can simply set AllowRedirects to True and let the TFPHTTPClient deal with it, as Thaddy said.
« Last Edit: November 11, 2018, 12:59:20 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Download a file from a redirection web server with TFPHTTPClient
« Reply #5 on: November 11, 2018, 12:57:53 am »
but i can force the GET method for 301,302,307 and 308? how i can force to accept bad redirect standard?
Technically yes, by overriding RedirectForcesGET method, which currently only checks for 303, to accept the other 30X. But you better consult the server response first.

Segator

  • Full Member
  • ***
  • Posts: 168
    • https://github.com/Nenirey
Re: Download a file from a redirection web server with TFPHTTPClient
« Reply #6 on: November 14, 2018, 12:46:14 am »
Thanks for help to solve it, In Lazarus/Free Pascal Trunk the redirection function check for 30x.
i am Reinier, Nenirey and Segator :) https://github.com/Nenirey

 

TinyPortal © 2005-2018