我有几个在所有浏览器中都能正常工作的 URL,但如果我尝试使用 Indy Http 客户端的 Get() 获取页面内容,它会返回错误代码 500,即内部服务器错误。这是最新的 Indy SVN 版本 (4981)。
这是我的示例代码。为此,所需要的只是带有 Indy 组件的 Delphi 以及带有按钮和备忘录的表单。
procedure TForm1.Button1Click(Sender: TObject);
var HTTPCLIENT1: TIdHTTP;
begin
try
try
HTTPCLIENT1 := TIdHTTP.Create(nil);
Memo1.Clear;
with HTTPCLIENT1 do
begin
HandleRedirects := True;
Request.UserAgent := 'Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.7.3) Gecko/20040924 Epiphany/1.4.4 (Ubuntu)';
Memo1.Text := Get('http://www.laredoute.fr/vente-machine-a-coudre-bernette-20-kit-couture--garantie-2-ans.aspx?productid=401225048&documentid=999999&categoryid=22918417&customertarget=0&offertype=0&prodcolor=1#pos=33_n_n_n_n_n_n&numberpage=2');
Caption := ResponseText;
end;
except
On e: Exception do
begin
Memo1.Lines.Add('Exception: '+e.Message);
end;
end;
finally
HTTPCLIENT1.Free;
end;
end;
这对我来说不是连接问题,因为 99% 的 URL 返回 200 或 404,只有少数返回 500,但每个浏览器都能在一秒钟内正常打开它们。
最佳答案
这种失败通常表明 GET
请求在某种程度上存在格式错误,导致服务器代码最终失败。但是,如果不了解网络浏览器请求与 TIdHTTP 请求的实际情况进行比较,就无法确定服务器不喜欢什么。
更新:我看到发生的情况是,当网络浏览器请求 URL 时,服务器会立即发回 200 响应,但是当 TIdHTTP 请求 URL 时,服务器会发送 301 重定向到新的 URL。 URL,然后在 TIdHTTP 请求该 URL 时发送 302 重定向到错误页面,然后在 TIdHTTP 请求该 URL 时发送 500 响应。
网络浏览器请求和初始 TIdHTTP 请求之间对网络服务器产生影响的两个区别是:
您使用 TIdHTTP 请求的 URL 末尾包含一个 anchor 标记(
#
字符之后的所有内容 -#pos=33_n_n_n_n_n_n&numberpage=2
),网络浏览器将使用该 anchor 标记通常会剥离。 anchor 实际上并不是 URL 的一部分。它们供网络浏览器在从 URL 检索的数据中定位点时使用。用户代理。一些网络服务器对不同的用户代理敏感,并且可以向不同类型的用户代理发送不同的响应。
当我从 URL 中删除 anchor 时,TIdHTTP.Get() 不再崩溃:
Memo1.Text := Get('http://www.laredoute.fr/vente-machine-a-coudre-bernette-20-kit-couture--garantie-2-ans.aspx?productid=401225048&documentid=999999&categoryid=22918417&customertarget=0&offertype=0&prodcolor=1');
关于delphi - 为什么 Indy 项目 HttpClient Get() 在某些在 Web 浏览器中正常工作的 URL 上给出代码 500?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15935811/