opc0de
opc0de

Reputation: 11768

What should I do about "302 Found" exceptions when downloading with Indy?

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

Answers (2)

Remy Lebeau
Remy Lebeau

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

David Grayson
David Grayson

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

Related Questions