本文介绍了Delphi 中 Internet Direct 组件在 POST 请求后 WWW 服务器报告错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Delphi XE4,我通常使用带有 IdHttp.POST 的 Indy 向网站发送 POST 请求,

I'm using Delphi XE4 and i usually use Indy with IdHttp.POST to POST request to websites,

这一次,每当我尝试 POST 请求时,我都会收到 错误:您的浏览器没有发送正确的数据.

This time, whenever i try to POST the request i get Error: Your browser is not sending the correct data.

我非常确定我正在发布正确的数据,并且我正在使用 IOHandler 和 CookieManager.

I'm very sure that I'm POSTing the right data, and i'm using the IOHandler and CookieManager.

处理这个问题好几天了(字面意思)

Been dealing with this for days(literally)

这是代码(代码中的站点):

Here is the code(the site in the code):

procedure TForm1.Button1Click(Sender: TObject);
var s, lge, Kf1, Kf2, Kf3, Kf4 : String;
    lParam                     : TStringList;
begin
  S := http.Get('https://www.neobux.com/m/l/');
  Memo1.Lines.Add(S);
  getParamLge(s,lge,'lge');
  GetInput(s,Kf1,'id="Kf1"');
  GetInput(s,Kf2,'id="Kf2"');
  GetInput(s,Kf3,'id="Kf3"');
  GetInput(s,Kf4,'id="Kf4"');


  lParam := TStringList.Create;
  lParam.Add('lge='+lge);
  lParam.Add(Kf1+'=USERNAME');
  lParam.Add(Kf2+'=PASSWORD');
  lParam.Add(Kf3+'=');
  lParam.Add(Kf4+'=');
  lParam.Add('login=1');


  memo1.Lines.Add(http.Post('https://www.neobux.com/m/l/', lParam));
end;

(getParamLge 和 GetInput 函数只是简单的复制和 pos 函数,用于从 GET 响应中提取值).

(the getParamLge and GetInput function, are just simple copy and pos functions to extract value from the GET respone).

我认为它可能需要 cookie,所以我在开始时添加了这个:

I thought maybe it needed cookies so i've added this in the beginning:

  Cookie.CookieCollection.Clear;
  Cookie.CookieCollection.AddClientCookies('CFID=21531887; CFTOKEN=20369251; dh=20130709111845,1920x1080,{ts ''2013-07-09 06:18:58''}; __utma=90161412.436822896.1373368451.1373368451.1373368451.1; __utmb=90161412.11.10.1373368451; __utmc=90161412; __utmz=90161412.1373368451.1.1.'+'utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __asc=06ff77ad13fc32381fd1f5d6405; __auc=06ff77ad13fc32381fd1f5d6405; __atuvc=4%7C28; MS=flat');

但一切都是徒劳的.

推荐答案

因为它不起作用 - 显然你不能(或 Delphi 不能 - 这对服务器没有区别).

Since it does not work - obviously you do not (or Delphi does not - that makes no difference for server).

你应该开始通常的调试循环:

You should start usual debugging loop:

  1. 观察参考工作行为.
  2. 观察您的程序行为
  3. 找出不同之处
  4. 消除差异
  5. 检查程序现在是否有效
  6. 如果不是 - 转到第 2 步.

参考实现将是一些与站点一起工作的WWW 浏览器:Opera、Chrome、Firefox、MS IE 等

Reference implementation would be some WWW browser working with site: Opera, Chrome, Firefox, MS IE, etc.

  • 观察工具可能是一些 HTTP Sniffer,例如 WireShark 或 OmniPacket 或 Microsoft Net Monitor 或其他,但是这会在相当深的层次上修补操作系统的工作.
  • 或者它可以是带有 GUI 的本地代理,例如 Proxomitron 或 Membrane Monitor - 但这需要对程序和浏览器进行特殊设置,以通过该本地代理路由其流量.
  • Observing tool would be some HTTP Sniffer like WireShark or OmniPacket or Microsoft Net Monitor or else, however this tinkers with OS work on rather deep level.
  • Or it can be local proxy with GUI, like Proxomitron or Membrane Monitor - but that would require special setup for both the program and the browser, to route their traffic through that local proxy.

然后您应该阅读有关 HTTP 的内容,从 Wikipedia 上的浅层观察开始,然后打开相关的 RFC 文档(HTTP 协议不同部分的规范),以便您了解观察到的差异意味着什么以及如何修复它们.例如,许多人在实际应该使用 GET 请求等时使用 POST 请求.

Then you should read about HTTP, starting with shallow observation at Wikipedia and then opening related RFC documents (specifications of different part of HTTP protocol) so that you would understand what do the observed differences mean and how to fix them. For example many people use POST request when they actually should use GET request or such.

您想调试 HTTP 程序,但对于此 HTTP 日志,workign 和 borken 是必需的,而您的问题缺少它们.更重要的是,您很可能可以自行修复它,只需将程序的 HTTP 日志符合 RFC 理论和浏览器工作实践即可.

You want to debug HTTP program but for this HTTP logs, workign and borken, are required and your question lacks them. More so, most probably you can fix it your self, just bring your program's HTTP log to accordance with both RFCs theory and working browsers practice.

这篇关于Delphi 中 Internet Direct 组件在 POST 请求后 WWW 服务器报告错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 10:07