Http学习笔记——完整的请求报文和Form表单

完整的请求报文包含3部分:起始行,header,body. body可以为空,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
    GET /wp-admin/post.php?post=432&action=edit HTTP/1.1 // 起始行,包含请求路径,http版本等信息,以空格分割,CRLF结尾
// 以下为header内容
Host    www.cxylg.com // 请求的host
Upgrade-Insecure-Requests   1
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36 // 客户端的各种信息
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 // 协商支持的内容
DNT 1
Referer http://www.cxylg.com/wp-admin/edit.php
Accept-Encoding gzip, deflate // 编码
Accept-Language zh,en;q=0.8,zh-CN;q=0.6,zh-TW;q=0.4 // 语言
Cookie  wordpress_e0e427ab5a5d8a0f7c6db73a9e886ff5=gongjiehong%7C1502796575%7Cjf9m06FiTD02MAbqzP58HEt6cU1d1SXt0LKEv59cn8S%7C9ba96b5b43027153835f1532148ccaa1d4ffe9eb31275a54d695787275ae264c; wordpress_logged_in_e0e427ab5a5d8a0f7c6db73a9e886ff5=gongjiehong%7C1502796575%7Cjf9m06FiTD02MAbqzP58HEt6cU1d1SXt0LKEv59cn8S%7C8d7e29436e44705a9221b06b493ff726fe847959afa2d0ef8371b270e510546d; wp-settings-1=deleted%3Dundefined%26mfold%3Do%26libraryContent%3Dbrowse%26posts_list_mode%3Dlist%26hidetb%3D1%26editor%3Dhtml%26post_dfw%3Doff%26wplink%3D0%26editor_plain_text_paste_warning%3D1; wp-settings-time-1=1501809865
// 这是一个典型的GET请求包,没有body

返回报文大同小异

1
2
3
4
5
6
7
8
9
10
11
12
    HTTP/1.1 200 OK // 起始行,包含HTTP版本,状态码
Server  Tengine/2.2.0 // 服务器信息
Date    Fri, 04 Aug 2017 02:29:48 GMT // 服务器时间
Content-Type    application/json // 返回的body数据类型
Content-Length  703 // 返回数据长度,不计header 和起始行
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control   no-store, no-cache, must-revalidate
Pragma  no-cache
Connection  Keep-alive

// body
{"locations":[{"id":"21","name":"\u9999\u6e2f\u7ebf\u8def1[\u4e3b\u529b]","mode":"cnout","ipv6_support":0},{"id":"36","name":"\u65e5\u672c\u7ebf\u8def3","mode":"cnout","ipv6_support":1},{"id":"31","name":"\u65b0\u52a0\u5761\u7ebf\u8def2[\u4e3b\u529b]","mode":"cnout","ipv6_support":0,"current":1}],"server_ip":"47.88.194.124","server_ip6":"","proxy_port":"7700","proxy_port6":"993","location_mode":"cnout","notify":null,"cnout_route_version":"1500169344","cnin_route_version":"1495940914","route_url":"https:\/\/swordfish.oss-cn-hangzhou.aliyuncs.com\/routes\/%@_%@.route","check_interval":300,"ipv6_test_ip":"2400:da00::dbf:0:100","ipv6_test_port":"80","ipv4_master_port":"7700","ipv4_slave_port":"25"}

大多数场景下我们可以将请求参数组织为JSON或者XML property list的方式放在HTTPBody中进行请求。但如果需要上传多个文件时,通常采用Form表单的形式。Form表单对比一般HTTP报文增加了boundary来分隔不同区块的数据,每个区块的数据定义了自己的数据header。示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
起始行 CRLF

header1 CRLF
header2 CRLF
... CRLF
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryrGKCBY7qhFd3TrwA CRLF // 此处header中定义数据类型为multipart/form-data,数据以WebKitFormBoundaryrGKCBY7qhFd3TrwA分隔
header* CRLF

// 紧接着是HTTPBody
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="text"

title
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="file"; filename="chrome.png" // 文件的信息,此处需要注意的是很多服务器语言在遍历多张图的时候需要把name="file" 改为name="file[]"才能正常取出多个文件
Content-Type: image/png // MIME

PNG ... content of chrome.png ...
------WebKitFormBoundaryrGKCBY7qhFd3TrwA--

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注