Reputation: 11768
I am trying to download a file to a string:
function FetchUrl(const url: string): string;
var
idhttp : TIdHTTP;
begin
idhttp := TIdHTTP.Create(nil);
try
Result := idhttp.Get(url);
finally
idhttp.Free;
end;
end;
What is wrong with this code? I get an exception: HTTP/1.1 302 Found
Upvotes: 5
Views: 5736
Reputation: 596246
Set the TIdHTTP.HandleRedirects
property to True. It is False by default.
function FetchUrl(const url: string): string;
var
idhttp : TIdHTTP;
begin
idhttp := TIdHTTP.Create(nil);
try
idhttp.HandleRedirects := True;
Result := idhttp.Get(url);
finally
idhttp.Free;
end;
end;
Upvotes: 12
Reputation: 87406
The HTTP response code 302 means the remote server wants to redirect you to another URL, which is specified in the Location header of the response. You need to do something special to look at the Location header and go to that URL. Maybe your http library has an option to do this automatically, so check the documentation.
Upvotes: 2