Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Mar : Re: TIdHttp and redirection
| Subject: | Re: TIdHttp and redirection |
| Posted by: | "Remy Lebeau (TeamB)" (no.spam@no.spam.com) |
| Date: | Mon, 20 Mar 2006 12:07:46 |
"Shiva" <shiva@gonzo.com> wrote in message
news:441f00d6$1@newsgroups.borland.com...
> I'd like to get the redirected address after a redirection
> (302 HTTP response message).
You need to use the TIdHTTP's OnRedirect event for that.
Alternatively, set the HandleRedirects property to false and then wrap the
Get() in a try..except block. When an EIdHTTPProtocolException with a
ReplyErrorCode of 302 is caught, you can look at the TIdHTTP's
Response.Location property and then issue a new request, ie:
function TDownloadThread.HttpGetHtml(var aAddress: string; var
aAddressOnPage: string; var aHTML: string; aMethod: string; var
aRedirectedAddress: string): boolean;
var
LAddress: String;
HTTP : TIdHTTP;
begin
LAddress := aAddress;
HTTP := TIdHTTP.Create(nil);
try
HTTP.HandleRedirects := False;
repeat
try
aHTML := HTTP.Get(LAddress);
Break;
except
on E: EIdHTTPProtocolException do
begin
LAddress := HTTP.Response.Location;
if LAddress = '' then raise;
end;
end;
until False;
finally
aRedirectedAddress := LAddress;
HTTP.Free;
end;
end;
> Unfortunately, this piece of code does not work:
For future reference, just saying that something does not work says nothing
about the actual problem you are having. Always provide specific details.
In this particular case, it doesn't work because the Response.ResponseCode
will be overriden with a new value on each new request that TIdHTTP issues
to the server. So your code will never see the 302 response at all. It
will be the 200 response from the final URL instead.
Gambit
none