Socket实现发送Http请求上传文件

来源:互联网 发布:枪械介绍app软件 编辑:程序博客网 时间:2024/05/16 16:10

最近在实现发送反馈的功能,需要自己封装实现Http上传,首先需要知道上传表单数据的格式,下面是我获取的请求格式

Remote Address:127.0.0.1:80
Request URL:http://127.0.0.1/test/welcome.php
Request Method:POST
Status Code:200 OK
Response Headers
view source
Connection:Keep-Alive
Content-Length:618
Content-Type:text/html
Date:Fri, 04 Aug 2017 03:10:32 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.4.45
X-Powered-By:PHP/5.4.45
Request Headers
view source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:591892
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary89hoWoA4ABt14mYg
Host:127.0.0.1
Origin:http://127.0.0.1
Referer:http://127.0.0.1/test/form.html
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Request Payload
------WebKitFormBoundary89hoWoA4ABt14mYg
Content-Disposition: form-data; name="comment"


无赖,无赖,江湖要我背对着你,啊,狂风呀,暴雨,狂风啊,暴雨,别得意,有什么了不起,我就此发誓,挥刀杀到天边去
------WebKitFormBoundary89hoWoA4ABt14mYg
Content-Disposition: form-data; name="name"


xinxin
------WebKitFormBoundary89hoWoA4ABt14mYg
Content-Disposition: form-data; name="email"


13234234@qq.com
------WebKitFormBoundary89hoWoA4ABt14mYg
Content-Disposition: form-data; name="file"; filename="汇编记录.doc"
Content-Type: application/msword


------WebKitFormBoundary89hoWoA4ABt14mYg--


上传文件到服务器最主要的是按上面的请求格式封装好,然后再去发送请求数据

请求头的格式:

POST /test/upload.php HTTP/1.1
Host: 127.0.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://127.0.0.1/test/form.html
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Content-Type: multipart/form-data; boundary=----AOMEIBackupForBoundary8oZtvmhb0OlCkGLo
Content-Length: 321530605


实例代码下载地址:

http://download.csdn.net/download/slfkj/9929422

原创粉丝点击