maxfax
maxfax

Reputation: 4305

Delphi: download a file with URL Forwarding

I have a free domain name with URL Forwarding (Cloaking) to another site.

If I type http://my.com/1.zip in browser's web-address then it goes to http://his.com/1.zip and downloads a file.

How can I do the same with Indy TIdHTTP (Delphi XE2). Browsers and I get 404-error at first but then they somehow download a file except me.

I need to use the first link but actually download from another site. E.g. the first site has a xxx.zip file. I want to go to http://my.com/xxx.zip but actually to download from http://his.com/xxx.zip (where the file stores).

Thanks!

Edited:

I set HandleRedirects to true, assigned a CookieManager (I've already seen this question Indy - IdHttp how to handle page redirects?).

Try to download this http://liga-updates.ua.tc/GDI+.zip in your Delphi

Upvotes: 1

Views: 6678

Answers (3)

Remy Lebeau
Remy Lebeau

Reputation: 595349

The website in question is returning an HTTP 404 response with an HTML page containing an <iframe> that loads the real URL. A 404 reply will cause TIdHTTP to raise an EIdHTTPProtocolException exception by default. The content of the reply (the HTML) can be accessed via the EIdHTTPProtocolException.ErrorMessage property.

For example:

procedure TForm1.Button1Click(Sender: TObject); 
var 
  Http: TIdHttp; 
  URL, Filename: string; 
  FS: TFileStream; 
  ...
begin 
  Filename := 'C:\path\GDI+.zip';
  URL := 'http://liga-updates.ua.tc/GDI+.zip'; 

  FS := TFileStream.Create(Filename, fmCreate); 
  try
    try
      Http := TIdHttp.Create(nil); 
      try 
        try
          Http.Get(URL, FS); 
        except 
          on E: EIdHTTPProtocolException do begin
            if E.ErrorCode <> 404 then raise;
            URL := ParseIFrameURLFromHTML(E.ErrorMessage);
            if URL = '' then raise;
            Http.Get(URL, FS); 
          end;
        end; 
      finally 
        Http.Free; 
      end; 
    finally 
      FS.Free; 
    end; 
  except
    DeleteFile(Filename);
    ShowMessage('Unable to download file.');
    Exit;
  end;
  ShowMessage('Downloaded OK'); 
end;

Upvotes: 3

kobik
kobik

Reputation: 21232

It seems that http://liga-updates.ua.tc is based on 404 error redirects to custom pages (used internally by the web-server).

Try to do http head on any resource there: it will return 404 with HTML response. that response holds an iframe element with src to the real download file. based on this, I wrote a small code.

I used THttpCli because it seems TIdHttp will not return a "valid" Response with status 404 (not in my D5 version anyway).

uses HttpProt;

procedure TForm1.Button1Click(Sender: TObject);
const
  IFRAME_SRC = '<iframe src="';
var
  HttpCli: THttpCli;
  S, URL, FileName: string;
  I: Integer;
  FS: TFileStream;
begin
  URL := 'http://liga-updates.ua.tc/GDI+.zip';

  HttpCli := THttpCli.Create(nil);
  try
    HttpCli.URL := URL;
    HttpCli.MultiThreaded := True;
    try
      HttpCli.Get;
    except
      // this will always be 404 for this domain (test from outside the IDE)
    end;
    S := HttpCli.LastResponse; // THttpCli returns valid response when status 404
    // extract IFRAME src
    I := Pos(IFRAME_SRC, S);
    if I <> 0 then
    begin
      Delete(S, 1, I + Length(IFRAME_SRC) - 1);
      URL := Copy(S, 1, Pos('"', S) - 1);
      HttpCli.URL := URL;
      FileName := ExtractFileName(StringReplace(URL, '/', '\', [rfReplaceAll]));
      FS := TFileStream.Create(FileName, fmCreate);
      try
        HttpCli.RcvdStream := FS;
        try
          HttpCli.Get;
          ShowMessage('Downaloded OK');
        except
          ShowMessage('Unable to download file.');
        end;
      finally
        FS.Free;
      end;
    end
    else
      ShowMessage('Unable to extract download information.');
  finally
    HttpCli.Free;
  end;
end;

Upvotes: 1

Pateman
Pateman

Reputation: 2757

Try the HandleRedirects property of the TIdHTTP component.

Upvotes: 0

Related Questions