6

我有几个在所有浏览器中都可以正常工作的 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,但每个浏览器都会在一秒钟内打开它们。

4

1 回答 1

10

这种失败通常表明GET请求以某种方式格式错误,导致服务器代码最终失败。但是,如果没有看到 Webbrowser 请求与 TIdHTTP 的请求相比实际上是什么样子,就无法确定服务器不喜欢什么。

更新:我看到发生的事情是,当网络浏览器请求 URL 时,服务器立即发回 200 响应,但是当 TIdHTTP 请求 URL 时,服务器发送 301 重定向到新 URL,然后发送 302 重定向到TIdHTTP 请求该 URL 时的错误页面,然后当 TIdHTTP 请求该 URL 时发送 500 响应。

webbrowser 请求和会影响 webserver 的初始 TIdHTTP 请求之间的两个区别是:

  1. 您使用 TIdHTTP 请求的 URL 在末尾包含一个锚标记(#字符 -之后的所有内容#pos=33_n_n_n_n_n_n&numberpage=2),网络浏览器通常会删除该标记。锚点实际上不是 URL 的一部分。它们旨在供网络浏览器在从 URL 检索的数据中定位点时使用。

  2. 用户代理。一些 web 服务器对不同的用户代理很敏感,并且可以向不同类型的用户代理发送不同的响应。

当我从 URL 中删除锚点时, 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');
于 2013-04-10T20:55:16.340 回答