說明
本篇主要是為了記錄HTTP中封包的格式,以便針對封包進行解析。首先會介紹基礎的HTTP封包,之後會介紹
檔案上傳時的資料封包格式.
HTTP基礎封包格式
按照HTTP封包類型進行介紹,HTTP封包類型分為請求封包(用戶端主動請求封包)和響應封包(服務端響應用戶端的回傳封包)。其基本的格式為:
GET / HTTP/
Host: www.enjoytoday.cn
Connection: keep-alive
Upgrade-Insecure-Requests:
User-Agent: Mozilla/ (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://www.enjoytoday.cn/posts/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: bdshare_firstime=; UM_distinctid=15c4ef2ac4e2e4-0d13269271b947-1b2a120b-1fa400-15c4ef2ac4f7b5; un=aGZjYWk=; comment_author=aGZjYWk=; [email protected]; comment_author_url=http://www.enjoytoday.cn; c_id=dUhIaTlndmc4MVVYbjRQTGxMRTotMTpFODg3QjgzQjg1NjgxQjQxRUYxNjg2QzJFRkMyQjI2QQ==; JSESSIONID=ADBC8C3DADF6C815D778450C193C6637.ajp13_worker; Hm_lvt_ce55bfda158556585a8b7b246346c8ba=1498560244,1498739070,1498833193,1498917432; Hm_lpvt_ce55bfda158556585a8b7b246346c8ba=1498917597; CNZZDATA1262047894=1598545996-1495973145-%7C1498917578
username=hfcai&sex=man
1) 請求封包
請求封包資料格式由三個部分組成:
-
請求方法、URI、HTTP協定版本
該部分位于資料首行,基本格式為:
該部分給出了請求類型和請求的資源位置(/index.html),其中HTTP中的請求類型包括:GET、POST、HEAD、PUT、DELETE.一般常用的為POST和GET方式。
-
請求頭部
該部分緊跟着上一部分(方法、協定版本行),該部分主要是用于描述請求正文,其基本格式如下:
主要是用于說明請求源、連接配接類型、以及一些Cookie資訊等。Host: www.enjoytoday.cn Connection: keep-alive Upgrade-Insecure-Requests: User-Agent: Mozilla/ (X11; Linux x86_64) AppleWebKit/ (KHTML, like Gecko) Chrome/ Safari/ ......
-
請求正文
請求正文和請求頭部通過一個空行進行隔開,一般用于存放POST請求類型的請求正文,如上的請求體為:
2)響應封包
響應封包和請求封包格式類型,同樣也是分為三個部分,響應頭部和響應體同樣也是通過一個空行進行隔開,如下為一個簡單的示例:
HTTP/1.1 OK
Date: Sat, 01 Jul 2017 14:51:26 GMT
Server: Apache/2.4.7 (Ubuntu)
Set-Cookie: JSESSIONID=84C993F5E433C4DE9BFBA57150FFC065.ajp13_worker;path=/;HttpOnly
Content-Language: zh-CN
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 7333
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html;charset=UTF-8
<html>
<head>
<title>title of html.</html>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
-
狀态碼部分
該部分主要給出響應HTTP協定版本号、響應傳回狀态碼、響應描述,同樣是單行顯示.格式為:
響應傳回一般由一個三位數組成,分别以整數1~5開頭組成。各個類型響應對應含義:
- 1XX:資訊提示,臨時的響應
- 2XX:請求成功,服務端已經接受到請求資訊
- 3XX:重定位
- 4XX:用戶端異常
-
5XX:服務端異常
常見的響應有:200(響應成功),400(請求異常,一般為參數異常),404(請求資源不存在),405(請求方式不支援),500(伺服器内部異常)
-
響應頭部
響應頭部主要是傳回一些伺服器的基本資訊,以及一些Cookie值等。如上的響應頭為:
Date: Sat, 01 Jul 2017 14:51:26 GMT Server: Apache/2.4.7 (Ubuntu) Set-Cookie: JSESSIONID=84C993F5E433C4DE9BFBA57150FFC065.ajp13_worker;path=/;HttpOnly Content-Language: zh-CN Vary: Accept-Encoding Content-Encoding: gzip Content-Length: 7333 Keep-Alive: timeout=5, max=100 Connection: Keep-Alive Content-Type: text/html;charset=UTF-8
-
響應體
該部分為請求需要得到的具體資料,可以為任何類型資料,一般網頁浏覽傳回的為html檔案内容.如上,内容不再列出。同樣,和響應頭部通過一個空行進行隔開。
檔案上傳封包格式
如下,介紹通過浏覽器上傳檔案時它的封包格式和一般的封包的異同。檔案上傳主要在于檔案上傳時的格式差異,如下給出一個示例的格式:
POST /servlet/UploadServlet HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 349
Cache-Control: max-age=0
Origin: http://localhost:8080
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryy4ceMstxB6tXIWBT
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://localhost:8080/hello.html
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.8
Cookie: comment_author="aGZjYWk="; comment_author_email="[email protected]"; comment_author_url="http://localhost:8080"; UM_distinctid=15c4ef587620-0ad25ac342d545-1b2a120b-1fa400-15c4ef58763717; CNZZDATA1262047894=628334530-1495973332-%7C1495973678; Hm_lvt_ce55bfda158556585a8b7b246346c8ba=1495973333
------WebKitFormBoundaryy4ceMstxB6tXIWBT
Content-Disposition: form-data; name="filedata"; filename="test.txt"
Content-Type: text/plain
This is the upload file
this is a test file
this is a text file
------WebKitFormBoundaryy4ceMstxB6tXIWBT
Content-Disposition: form-data; name="submit"
upload
------WebKitFormBoundaryy4ceMstxB6tXIWBT--
如上,我上傳了一個測試檔案(test.txt),其檔案内容為:
This is the upload file
this is a test file
this is a text file
其基本格式還是遵循HTTP的請求格式,但對于正文部分添加了一些對于上傳檔案的說明,格式如下:
POST /servlet/UploadServlet HTTP/ #請求uri、方法
Host: localhost:Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryy4ceMstxB6tXIWBT #邊界值
......
Accept-Language: zh-CN,zh;q=
------WebKitFormBoundaryy4ceMstxB6tXIWBT #邊界
Content-Disposition: form-data;name="filedata";filename="test.txt" #檔案基本資訊
Content-Type: text/plain #檔案基本資訊
This is the upload file #檔案内容
this is a test file
this is a text file
------WebKitFormBoundaryy4ceMstxB6tXIWBT #送出按鈕屬性資訊部分
Content-Disposition: form-data; name="submit"
upload #送出按鈕value值
------WebKitFormBoundaryy4ceMstxB6tXIWBT--
其他部分和一般的HTTP資料格式一樣(包括響應資料的格式)。
Enjoytoday,Enjoycoding