具有多个文件的Multipart HTTP请求应该是什么样的?

我正在做一个iPhone应用程序,使多个图像文件的多部分HTTP请求。

它看起来像是在服务器端发生的事情是,其中一个图像得到正确parsing,但其他两个文件不是。

任何人都可以发布一个包含多个图像文件的示例HTTP多部分请求?

那么请注意,该请求包含二进制数据,所以我不发布请求 – 相反,我已经将每个non-printable-ascii字符转换为点(“。”)。

POST /cgi-bin/qtest HTTP/1.1 Host: aram User-Agent: Mozilla/5.0 Gecko/2009042316 Firefox/3.0.10 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://aram/~martind/banner.htm Content-Type: multipart/form-data; boundary=----------287032381131322 Content-Length: 514 ------------287032381131322 Content-Disposition: form-data; name="datafile1"; filename="r.gif" Content-Type: image/gif GIF87a.............,...........D..; ------------287032381131322 Content-Disposition: form-data; name="datafile2"; filename="g.gif" Content-Type: image/gif GIF87a.............,...........D..; ------------287032381131322 Content-Disposition: form-data; name="datafile3"; filename="b.gif" Content-Type: image/gif GIF87a.............,...........D..; ------------287032381131322-- 

请注意,每行(包括以“322–”结尾的最后一行)都以\ r \ n序列结尾。

编辑 :我正在维护一个类似的,但更深入的答案在: https : //stackoverflow.com/a/28380690/895245

要查看到底发生了什么,请使用nc -l和用户代理(如浏览器或cURL)。

将表单保存到.html文件中:

 <form action="http://localhost:8000" method="post" enctype="multipart/form-data"> <p><input type="text" name="text" value="text default"> <p><input type="file" name="file1"> <p><input type="file" name="file2"> <p><button type="submit">Submit</button> </form> 

创build要上传的文件:

 echo 'Content of a.txt.' > a.txt echo '<!DOCTYPE html><title>Content of a.html.</title>' > a.html 

跑:

 nc -l localhost 8000 

在浏览器中打开HTML,select文件并点击提交并检查terminal。

nc打印收到的请求。 火狐发送:

 POST / HTTP/1.1 Host: localhost:8000 User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:29.0) Gecko/20100101 Firefox/29.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Cookie: __atuvc=34%7C7; permanent=0; _gitlab_session=226ad8a0be43681acf38c2fab9497240; __profilin=p%3Dt; request_method=GET Connection: keep-alive Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266 Content-Length: 554 -----------------------------9051914041544843365972754266 Content-Disposition: form-data; name="text" text default -----------------------------9051914041544843365972754266 Content-Disposition: form-data; name="file1"; filename="a.txt" Content-Type: text/plain Content of a.txt. -----------------------------9051914041544843365972754266 Content-Disposition: form-data; name="file2"; filename="a.html" Content-Type: text/html <!DOCTYPE html><title>Content of a.html.</title> -----------------------------9051914041544843365972754266-- 

或者,cURL应该发送与浏览器表单相同的POST请求:

 nc -l localhost 8000 curl -F "text=default" -F "file1=@a.html" -F "file1=@a.txt" localhost:8000 

您可以使用以下方式进行多项测

 while true; do printf '' | nc -l localhost 8000; done