使用Indy下载时,我应该如何处理“302 Found”异常?
问题描述:
我试图将文件下载到一个字符串:使用Indy下载时,我应该如何处理“302 Found”异常?
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找到
答
将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;
+0
作品谢谢;) – opc0de 2011-12-20 17:41:52
答
HTTP响应代码302意味着远程服务器想要将您重定向到另一个URL,该URL在响应的位置标题中指定。您需要做一些特殊的事情来查看位置标题并转到该URL。也许你的http库有一个选项可以自动完成,所以请检查文档。
如果我没有记错,有一个在TIdHTTP一个选项,允许您设置“AllowRedirects”或类似的东西,那么它会自动重定向,当然除了,如果有使用JavaScript的一些神奇的完成了... – ComputerSaysNo 2011-12-20 16:10:33
正确的属性名称是'HandleRedirects'。 – 2011-12-20 17:32:37
从https位置下载时出现同样的异常,但只有在中间有代理时。 https流量不应该通过代理透明传递吗?为什么我应该重定向?我把HandleRedirects保持为FALSE ... – Bozzy 2015-07-08 15:50:23