Recent

Author Topic: [SOLVED] how to get redirected url  (Read 2264 times)

fan2006

  • New member
  • *
  • Posts: 5
[SOLVED] how to get redirected url
« on: July 25, 2021, 03:55:21 am »
in c#,i can get the result from responseheaders("location") with set allowredirects to false.but in fp i will get an 302 error with the code "http.AllowRedirect := false".if i change it to true the code could continue but the responseheaders do not contain the info of location.how to solve it?
Code: Pascal  [Select][+][-]
  1. uses
  2.   FphttpClient,OpensslSockets,Classes;
  3.   var
  4.     http:TfphttpClient;
  5.     url:string;
  6.     response:TStringList;
  7. begin
  8.   url := 'http://www.baidu.com/link?url=ojjD2hHxviDl0j4T6MCQzRaQYUyYe0BX2aCXcNI5UliRtQum2Y7XH9_xZ08mzOJH';
  9.   http:=TfphttpClient.Create(nil);
  10.   http.AllowRedirect := true;
  11.   http.AddHeader('user-agent','Mozilla/5.0');
  12.   response:=TStringList.Create;
  13.   http.get(url,response);
  14.   writeln(http.ResponseHeaders.Text);
  15.   http.Free;
  16.   response.Free;
  17.   readln;
  18. end.      






[Edited to add SOLVED to topic]
« Last Edit: July 25, 2021, 11:27:45 am by trev »

rsz

  • New Member
  • *
  • Posts: 45
Re: how to get redirected url
« Reply #1 on: July 25, 2021, 08:34:10 am »
I believe the cleanest solution is to extend the TFPHTTPClient class and record/handle the redirects by setting the AllowRedirect to True and setting OnRedirect to a method where you handle the redirects. You could probably also just catch the 302 exception and manually handle redirects, but that seems hacky to me.

Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. uses
  4.   FphttpClient,OpensslSockets,Classes,sysutils;
  5.  
  6. type
  7.  
  8.   TMyHttpClient = class(TFPHTTPClient)
  9.   private
  10.     FRedirects: TStrings;
  11.     procedure HandleRedirect(Sender: TObject; Const ASrc: String;
  12.       Var ADest: String);
  13.   public
  14.     constructor Create(AOwner: TComponent); override;
  15.     destructor Destroy; override;
  16.     procedure HTTPMethod(const AMethod, AURL: String;
  17.       Stream: TStream; const AllowedResponseCodes: array of Integer); override;
  18.     property Redirects: TStrings read FRedirects;
  19.   end;
  20.  
  21.   constructor TMyHttpClient.Create(AOwner: TComponent);
  22.   begin
  23.     inherited;
  24.     FRedirects := TStringList.Create;
  25.     OnRedirect := @HandleRedirect;
  26.   end;
  27.  
  28.   destructor TMyHttpClient.Destroy;
  29.   begin
  30.     FRedirects.Free;
  31.     inherited;
  32.   end;
  33.  
  34.   procedure TMyHttpClient.HandleRedirect(Sender: TObject; Const ASrc: String; Var ADest: String);
  35.   begin
  36.     { do whatever you want per redirect, in this example we just add the
  37.       redirect destination url to a list of strings }
  38.     Redirects.Add(ADest);
  39.     { you can also access headers here if you like }
  40.     //WriteLn(GetHeader(ResponseHeaders, 'Location'));
  41.   end;
  42.  
  43.   procedure TMyHttpClient.HTTPMethod(const AMethod, AURL: String;
  44.       Stream: TStream; const AllowedResponseCodes: array of Integer);
  45.   begin
  46.     { clear redirects list before initiating requests }
  47.     Redirects.Clear;
  48.     inherited;
  49.   end;
  50.  
  51. var
  52.   http:TMyHttpClient;
  53.   url, RedirectUrl:string;
  54.   response:TStringList;
  55. begin
  56.   url := 'http://www.baidu.com/link?url=ojjD2hHxviDl0j4T6MCQzRaQYUyYe0BX2aCXcNI5UliRtQum2Y7XH9_xZ08mzOJH';
  57.   http:=TMyHttpClient.Create(nil);
  58.   http.AllowRedirect := True;
  59.   http.AddHeader('user-agent','Mozilla/5.0');
  60.   response:=TStringList.Create;
  61.   http.get(url,response);
  62.   writeln(http.ResponseHeaders.Text);
  63.  
  64.   WriteLn;
  65.   WriteLn('We were redirected ' + IntToStr(http.Redirects.Count) + ' times!');
  66.   for RedirectUrl in http.Redirects do
  67.   begin
  68.     WriteLn(RedirectUrl);
  69.   end;
  70.  
  71.   http.Free;
  72.   response.Free;
  73.   readln;
  74. end.
« Last Edit: July 27, 2021, 09:15:51 am by rsz »

fan2006

  • New member
  • *
  • Posts: 5
Re: how to get redirected url
« Reply #2 on: July 25, 2021, 09:24:19 am »
@rsz,thx for providing such a clear solution and it works fine.

 

TinyPortal © 2005-2018