如何在POST请求中模仿HTML表单提交?

Vio*_*ffe 22 forms post http http-post

我有HTML表单,用于从应用程序发送bug报告到服务器.我需要以编程方式模仿这种行为.相应的POST请求(或一系列请求)是什么样的?

<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> 
Run Code Online (Sandbox Code Playgroud)

Mar*_*tin 43

POST请求由许多标头和请求主体组成.提交表单时,浏览器URL会对所有表单字段的名称和值进行编码,然后以这种格式将它们放入请求正文中:

fieldname1=fieldvalue1&fieldname2=fieldvalue2
Run Code Online (Sandbox Code Playgroud)

即请求主体看起来像一个典型的查询字符串.


以下是您的表单请求的样子:

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=
Run Code Online (Sandbox Code Playgroud)

要确保您的程序与浏览器的功能相匹配,您可以使用Firefox发布表单,然后使用Firebug的网络面板检查请求标题和正文.