Forum > Networking and Web Programming
[SOLVED] how to get redirected url
(1/1)
fan2006:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses FphttpClient,OpensslSockets,Classes; var http:TfphttpClient; url:string; response:TStringList;begin url := 'http://www.baidu.com/link?url=ojjD2hHxviDl0j4T6MCQzRaQYUyYe0BX2aCXcNI5UliRtQum2Y7XH9_xZ08mzOJH'; 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:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.baidu.com/link?url=ojjD2hHxviDl0j4T6MCQzRaQYUyYe0BX2aCXcNI5UliRtQum2Y7XH9_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:
@rsz,thx for providing such a clear solution and it works fine.
Navigation
[0] Message Index