如何在POST请求中模仿HTML表单提交?
我有HTML表单,用于从应用程序向服务器发送bug报告。我需要以编程方式模仿这种行为。相应的POST请求(或一系列请求)是什么样的?
I have HTML form that is used for sending bugreports from application to server. I need to mimic this behavior programmatically. What will the corresponding POST request (or series of requests) look like?
<form name="bugreport" method="post" enctype="multipart/form-data" action="http://my-server.com/bugreport.php">
<div name="SentData">
<textarea name="logfile" class="UserVisible"></textarea><br>
<textarea name="configfile" class="UserVisible"></textarea><br>
</div>
<textarea name="usercomment" class="invisible"></textarea><br>
<input name="useremail" type="text" class="invisible">
<input class="invisible" type="submit" value="Send">
</form>
POST请求包含多个标题和一个请求机构。当您提交表单时,浏览器 URL编码所有表单字段的名称和值,然后将它们放入在此格式的请求正文中:
A POST request consists of a number of headers and a request body. When you submit a form, the browser URL encodes names and values of all form fields and then puts them in the request body in this format:
fieldname1=fieldvalue1&fieldname2=fieldvalue2
即请求正文看起来像一个典型的查询字符串。
I.e. the request body looks like a typical query string.
以下是表单的请求:
POST /bugreport.php HTTP/1.1
Host: www.example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: [size of the request body]
logfile=blabla&configfile=more+blabla&usercomment=hello&useremail=
为了确保您的程序符合浏览器的功能,您可以使用Firefox发布表单,然后使用 Firebug 的网络面板检查请求标题和正文。
To make sure your program matches what a browser would do, you can post the form with Firefox and then inspect the request headers and body using Firebug's net panel.