Delphi:使用 URL 转发下载文件

发布于 2024-12-26 17:04:25 字数 991 浏览 2 评论 0原文

我有一个免费域名,可将 URL 转发(伪装)到另一个网站。

如果我在浏览器的网址中输入 http://my.com/1.zip 那么它就会到 http://his.com/1.zip 并下载文件。

我怎样才能用 Indy TIdHTTP (Delphi XE2) 做同样的事情。浏览器和我一开始收到 404 错误,但后来他们以某种方式下载了除我之外的文件。

我需要使用第一个链接,但实际上是从另一个网站下载的。例如,第一个站点有一个 xxx.zip 文件。我想去 http://my.com/xxx.zip 但实际上是从 < a href="http://his.com/xxx.zip" rel="nofollow noreferrer">http://his.com/xxx.zip (文件存储位置)。

谢谢!

编辑:

我将 HandleRedirects 设置为 true,分配了一个 CookieManager (我已经看过这个问题 Indy - IdHttp 如何处理页面重定向?)。

尝试下载http://liga-updates.ua.tc /GDI+.zip 在你的 Delphi 中

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉南桥 2025-01-02 17:04:25

相关网站返回 HTTP 404 响应,其中包含一个 HTML 页面,其中包含加载真实 URL 的 。默认情况下,404 回复将导致 TIdHTTP 引发 EIdHTTPProtocolException 异常。可以通过 EIdHTTPProtocolException.ErrorMessage 属性访问回复的内容(HTML)。

例如:

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;

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;
清风不识月 2025-01-02 17:04:25

似乎 http://liga-updates.ua.tc 是基于 404 错误重定向到自定义页面(由网络服务器内部使用)。

尝试对那里的任何资源执行 http head :它将返回 404 和 HTML 响应。
该响应包含一个 iframe 元素,其中 src 指向真正的下载文件。
基于此,我写了一个小代码。

我使用了 THttpCli 因为 TIdHttp 似乎不会返回“有效”响应状态为 404(无论如何不在我的 D5 版本中)。

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;

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;
亽野灬性zι浪 2025-01-02 17:04:25

尝试使用 TIdHTTP 组件的 HandleRedirects 属性。

Try the HandleRedirects property of the TIdHTTP component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文