HTTP请求包文格式

来源:互联网 发布:怎样抢注域名 编辑:程序博客网 时间:2024/04/30 13:20

补充一下:

application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。
multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分。
text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。

通过抓包获取以下数据。根据这些数据和数据的格式,可以对网络编程的理解和网络请求框架的使用进行更加深刻的理解。



Post表单形式上传文件:(同时上传两个文件,每个文件对应一部分)

请求: //注意每一行的细节“–”、“换行”、“boundary的值”

POST http://125.216.242.147:8080/yjy/file/upload HTTP/1.1
Content-Type: multipart/form-data; boundary=da185eb8-c4f3-47af-82b9-92c031a9bb09
Content-Length: 729
Host: 125.216.242.147:8080 //目标地址
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.3.1

–da185eb8-c4f3-47af-82b9-92c031a9bb09 //开始设置参数:levelId=2
Content-Disposition: form-data; name=”levelId”//参数key:levelId
Content-Length: 1 //参数value的长度

2 //参数value:2
–da185eb8-c4f3-47af-82b9-92c031a9bb09 //开始设置参数:userId=402875……
Content-Disposition: form-data; name=”userId”
Content-Length: 32

40287581551beea001551bf5f29b0000
–da185eb8-c4f3-47af-82b9-92c031a9bb09 //添加文件、文件名、文件内容
Content-Disposition: form-data; name=”file”; filename=”test.txt” //注意:name 和filename 值的意义
Content-Type: text/plain //MIME类型:
Content-Length: 61 //文件的长度

01268845855584585586935888 //文件具体的内容……
this is my words
fhfh……

–da185eb8-c4f3-47af-82b9-92c031a9bb09
Content-Disposition: form-data; name=”file”; filename=”test2.txt”
Content-Type: text/plain
Content-Length: 58

01268845855584585586935888
this is my words
fghfhf
test2……

–da185eb8-c4f3-47af-82b9-92c031a9bb09– //注意最后还有个“ – ”,表示全部结束

响应:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Length: 566
Date: Fri, 19 Aug 2016 07:26:23 GMT

{“result”:true,”document”:{“docId”:”297ed60b56a159b70156a1b18ecc0003”,”originalName”:”test.txt”,”saveName”:”201608191526413400.txt”,”savePath”:”D:\image\201608191526413400.txt”,”downPath”:”http://125.216.242.147:8080/attachment/201608191526413400.txt“,”type”:”“,”publishStatus”:0,”needPay”:0,”money”:0,”docSize”:61,”uploadTime”:1471591583426,”user”:{“userId”:”40287581551beea001551bf5f29b0000”,”username”:null,”password”:null,”realName”:null,”avator”:null,”money”:0,”role”:null,”level”:null},”level”:{“levelId”:”2”,”levelName”:null,”levelDesc”:null,”users”:null}}}



将文件作为请求体,发送到服务器:

请求:

POST http://125.216.242.147:8080/yjy/file/upload HTTP/1.1
Content-Type: application/octet-stream
Content-Length: 61
Host: 125.216.242.147:8080
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.3.1

01268845855584585586935888
this is my words…… //文件具体内容

响应:

HTTP/1.1 500 Internal Server Error
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 4445
Date: Fri, 19 Aug 2016 07:28:32 GMT
Connection: close

<html><head><title>Apache Tomcat/7.0.54 - Erro…(省略)


Post传递参数

请求:
POST http://125.216.242.147:8080/yjy/file/list HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Content-Length: 55
Host: 125.216.242.147:8080
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.3.1

levelId=2&uploadUserId=40287581551beea001551bf5f29b0000 //参数的key和value,多个参数用“&”连接

响应:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 13736
Date: Sat, 20 Aug 2016 02:42:04 GMT

{“result”:true,”total”:24,”documents”:[{“docId”:”297ed60b5654a206015654d6f8560016”,”originalName”:”……}
//具体的Json串



下载文件时的接收到的报文格式:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Accept-Ranges: bytes
ETag: W/”61-1471675934041”
Last-Modified: Sat, 20 Aug 2016 06:52:14 GMT
Content-Type: text/plain(注:txt)
(注:word:Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document)
Content-Length: 61 //报文数据部分的长度,可以用来表示“所下载文件的大小”
Date: Tue, 06 Sep 2016 03:33:27 GMT

01268845855584585586935888
this is my words
……


0 0
原创粉丝点击