[SOLVED] how to get redirected url lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- fan2006 · 2021-07-25 01:55 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  FphttpClient,OpensslSockets,Classes;  var    http:TfphttpClient;    url:string;    response:TStringList;begin  url := 'http://www.baidu.co m/link?url=ojjD2hHxviDl0j4T6MCQzRaQYUyYe0BX2aCXcNI5UliRtQum2Y7XH9_xZ08 mzOJH';  http:=TfphttpClient.Create(nil);  http.AllowRedirect := true;  http.AddHeader('user-agent','Mozilla/5.0');  response:=TStringList.Create;  http.get(url,response);  writeln(http.ResponseHeaders.Text);  http.Free;  response.Free;  readln;end.       [Edited to add SOLVED to topic] ---------------------------------------------------------------------- rsz · 2021-07-25 06:34 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program project1; uses  FphttpClient,OpensslSockets,Classes,sysutils; type   TMyHttpClient = class(TFPHTTPClient)  private    FRedirects: TStrings;    procedure HandleRedirect(Sender: TObject; Const ASrc: String;      Var ADest: String);  public    constructor Create(AOwner: TComponent); override;    destructor Destroy; override;    procedure HTTPMethod(const AMethod, AURL: String;      Stream: TStream; const AllowedResponseCodes: array of Integer); override;    property Redirects: TStrings read FRedirects;  end;   constructor TMyHttpClient.Create(AOwner: TComponent);  begin    inherited;    FRedirects := TStringList.Create;    OnRedirect := @HandleRedirect;  end;   destructor TMyHttpClient.Destroy;  begin    FRedirects.Free;  inherited;  end;   procedure TMyHttpClient.HandleRedirect(Sender: TObject; Const ASrc: String; Var ADest: String);  begin    { do whatever you want per redirect, in this example we just add the      redirect destination url to a list of strings }    Redirects.Add(ADest);    { you can also access headers here if you like }    //WriteLn(GetHeader(ResponseHeaders, 'Location'));  end;   procedure TMyHttpClient.HTTPMethod(const AMethod, AURL: String;      Stream: TStream; const AllowedResponseCodes: array of Integer);  begin    { clear redirects list before initiating requests }    Redirects.Clear;    inherited;  end; var  http:TMyHttpClient;  url, RedirectUrl:string;  response:TStringList;begin  url := 'http://www.ba idu.com/link?url=ojjD2hHxviDl0j4T6MCQzRaQYUyYe0BX2aCXcNI5UliRtQum2Y7XH 9_xZ08mzOJH';  http:=TMyHttpClient.Create(nil);  http.AllowRedirect := True;  http.AddHeader('user-agent','Mozilla/5.0');  response:=TStringList.Create;  http.get(url,response);  writeln(http.ResponseHeaders.Text);   WriteLn;  WriteLn('We were redirected ' + IntToStr(http.Redirects.Count) + ' times!');  for RedirectUrl in http.Redirects do  begin    WriteLn(RedirectUrl);  end;   http.Free;  response.Free;  readln;end. ---------------------------------------------------------------------- fan2006 · 2021-07-25 07:24 @rsz,thx for providing such a clear solution and it works fine.