【问题标题】:What should I do about "302 Found" exceptions when downloading with Indy?使用 Indy 下载时出现“302 Found”异常怎么办?
【发布时间】:2011-12-20 15:35:31
【问题描述】:

我正在尝试将文件下载到字符串:

function FetchUrl(const url: string): string;
var
 idhttp : TIdHTTP;
begin
  idhttp := TIdHTTP.Create(nil);
  try
    Result := idhttp.Get(url);
  finally
    idhttp.Free;
  end;
end;

这段代码有什么问题?我得到一个例外:HTTP/1.1 302 Found

【问题讨论】:

  • 如果我没记错的话,TIdHTTP 中有一个选项允许您设置“AllowRedirects”或类似的东西,然后它会自动重定向,当然,如果使用 javascript 完成了一些魔法.. .
  • 正确的属性名称是HandleRedirects。
  • 我在从 https 位置下载时遇到了同样的异常,但只有当中间有代理时。 https流量不应该“透明地”通过代理吗?为什么我应该被重定向?我故意将 HandleRedirects 设置为 FALSE...

标签: delphi indy


【解决方案1】:

将TIdHTTP.HandleRedirects 属性设置为True。默认为 False。

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; 

【讨论】:

    【解决方案2】:

    HTTP 响应代码 302 表示远程服务器要将您重定向到另一个 URL,该 URL 在响应的 Location 标头中指定。您需要做一些特别的事情来查看 Location 标头并转到该 URL。也许您的 http 库可以选择自动执行此操作,因此请查看文档。

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      相关资源
      最近更新 更多