为什么Indy Project HttpClient Get()在某些在Web浏览器中正常工作的URL上给出代码500? [英] Why does Indy Project HttpClient Get() give code 500 on some URLs which work fine in web browsers?

查看:89
本文介绍了为什么Indy Project HttpClient Get()在某些在Web浏览器中正常工作的URL上给出代码500?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个在所有浏览器中都能正常工作的URL,但是如果尝试使用Indy Http客户端的Get()获取页面内容,它将返回错误代码500,内部服务器错误。这是最新的Indy SVN版本(4981)。

I have several URLs which work just fine in all browsers, but if I try to get the page content using Get() of the Indy Http client, it returns error code 500, internal server error. This is with the latest Indy SVN build (4981).

这是我的示例代码。所需的全部是带有Indy组件的Delphi,以及带有按钮和备注的表单。

Here is my example code. All that is needed for this is Delphi with Indy components and a form with a button and a memo.

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,只有很少的URL返回返回500,但是每个浏览器都可以在一秒钟内打开它们。

It's not a connection problem on my side, since 99% of URLs return 200 or 404, only few return 500, but every browser opens them fine in a second.

推荐答案

这种故障通常表明 GET 请求的格式有误,导致服务器代码最终失败。但是,如果没有看到Web浏览器请求的实际外观与TIdHTTP的请求进行比较,就无法确定服务器不喜欢什么。

That kind of failure usually suggests the GET request is malformed in some way, causing the server code to fail on its end. But without seeing what the webbrowser requests actually look like for comparison to TIdHTTP's requests, there is no way to know for sure what the server is not liking.

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

Update: what I see happening is that when a webbrowser requests the URL, the server sends back a 200 response immediately, however when TIdHTTP requests the URL, the server sends a 301 redirect to a new URL, which then sends a 302 redirect to an error page when TIdHTTP requests that URL, which then sends the 500 response when TIdHTTP requests that URL.

webbrowser请求与初始TIdHTTP请求之间的两个区别可能会对网络服务器产生影响的是:

The two differences between a webbrowser request and the initial TIdHTTP request that would have an effect on a webserver are:


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

  1. the URL you are requesting with TIdHTTP includes an anchor tag at the end (everything after the # character - #pos=33_n_n_n_n_n_n&numberpage=2) which webbrowsers would normally strip out. Anchors are not actually part of URLs. They are meant for webbrowsers to use when locating spots within data that is retrieved from a URL.

用户代理。某些Web服务器对不同的用户代理敏感,并且可以向不同类型的用户代理发送不同的响应。

the user agent. Some web servers are sensitive to different user agents, and can send different responses to different types of user agents.

何时我从URL中删除了锚点TIdHTTP.Get()不再崩溃:

When I remove the anchor from the URL, TIdHTTP.Get() no longer crashes:

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');

这篇关于为什么Indy Project HttpClient Get()在某些在Web浏览器中正常工作的URL上给出代码500?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆