简体   繁体   English

以编程方式发布表单并没有执行我的浏览器正在执行的操作。 为什么?

[英]Programatically POST'ing a form is not doing what my browser is doing. Why?

I'm trying to programmatically submit a form on a web site that I do not own. 我试图以编程方式在我不拥有的网站上提交表单。 I'm trying to simulate what I would manually do with a web browser. 我正在尝试模拟使用Web浏览器手动进行的操作。 I am issuing an HTTP POST request using an HTTP library. 我正在使用HTTP库发出HTTP POST请求。

For a reason that I don't know I am getting a different result (an error, a different response, ...) when I programmatically submit the form compared to a manual submission in a web browser. 出于某种原因,与通过Web浏览器手动提交表单相比,以编程方式提交表单时,我不知道会得到不同的结果(错误,不同的响应……)。

How can that be and how can I find out what mistake I have made? 那怎么可能?我该如何发现自己犯了什么错误?

This question is intentionally language and library agnostic. 这个问题是故意与语言和图书馆无关的。 I'm asking for the general procedure for debugging such issues. 我要求调试此类问题的一般步骤。

All instances of this problem are equivalent. 此问题的所有实例都是等效的。 Here is how to resolve all of them: 以下是解决所有问题的方法:

The web site you are posting to cannot tell different clients apart. 您要发布到的网站无法区分其他客户。 It cannot find out whether you are using a web browser or an HTTP library. 它无法确定您使用的是Web浏览器还是HTTP库。 Therefore, only what you send matters for the decision of the server on how to react . 因此, 只有您发送的内容才是服务器决定如何做出反应的关键

If you observe different responses from the server this means that you are sending different requests. 如果您观察到来自服务器的不同响应,则意味着您正在发送不同的请求。

A few important things that you probably have to send correctly: 您可能必须正确发送的一些重要事项:

  • URL 网址
  • Verb ( GET or POST ) 动词( GETPOST
  • Headers: Host , User-Agent , Content-Length 标头: HostUser-AgentContent-Length
  • Cookies (the Cookie and Set-Cookie headers) Cookies( CookieSet-Cookie标头)
  • The request body 请求主体

Use an HTTP sniffer like Fiddler to capture what you are programmatically sending and what your browser is sending. 使用像Fiddler这样的HTTP嗅探器来捕获您正在以编程方式发送的内容以及浏览器正在发送的内容。 Compare the requests for differences. 比较差异要求。 Eliminate the differences one by one to see which one caused the problem. 逐一消除差异,以找出导致问题的原因之一。 You can drag an HTTP request into the Composer Window to be able to modify and reissue it. 您可以将HTTP请求拖到Composer窗口中,以进行修改和重新发出。

It is impossible to still get a different result if you truly have eliminated all differences between the manual and the programmatic requests. 如果您确实消除了手动和程序化请求之间的所有差异,那么仍然不可能获得不同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM