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.