HTTP 协议报文解析
本篇主要是為了記錄HTTP中報(bào)文的格式,以便針對(duì)報(bào)文進(jìn)行解析。首先會(huì)介紹基礎(chǔ)的HTTP報(bào)文,之后會(huì)介紹?
文件上傳時(shí)的數(shù)據(jù)報(bào)文格式.
?
HTTP基礎(chǔ)報(bào)文格式
?
按照HTTP報(bào)文類型進(jìn)行介紹,HTTP報(bào)文類型分為請(qǐng)求報(bào)文(客戶端主動(dòng)請(qǐng)求報(bào)文)和響應(yīng)報(bào)文(服務(wù)端響應(yīng)客戶端的回傳報(bào)文)。其基本的格式為:
?
GET / HTTP/1.1
Host: www.enjoytoday.cn
Connection: keep-alive
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
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://www.enjoytoday.cn/posts/326
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: bdshare_firstime=1466032270994; UM_distinctid=15c4ef2ac4e2e4-0d13269271b947-1b2a120b-1fa400-15c4ef2ac4f7b5; un=aGZjYWk=; comment_author=aGZjYWk=; comment_author_email=1710600212@qq.com; 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) 請(qǐng)求報(bào)文
?
請(qǐng)求報(bào)文數(shù)據(jù)格式由三個(gè)部分組成:
?
請(qǐng)求方法、URI、HTTP協(xié)議版本
?
該部分位于數(shù)據(jù)首行,基本格式為:
?
GET /index.html HTTP/1.1
1
該部分給出了請(qǐng)求類型和請(qǐng)求的資源位置(/index.html),其中HTTP中的請(qǐng)求類型包括:GET、POST、HEAD、PUT、DELETE.一般常用的為POST和GET方式。
?
請(qǐng)求頭部
?
該部分緊跟著上一部分(方法、協(xié)議版本行),該部分主要是用于描述請(qǐng)求正文,其基本格式如下:
?
Host: www.enjoytoday.cn
Connection: keep-alive
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
......
?
主要是用于說(shuō)明請(qǐng)求源、連接類型、以及一些Cookie信息等。
?
請(qǐng)求正文
?
請(qǐng)求正文和請(qǐng)求頭部通過(guò)一個(gè)空行進(jìn)行隔開(kāi),一般用于存放POST請(qǐng)求類型的請(qǐng)求正文,如上的請(qǐng)求體為:
?
username=hfcai&sex=man
1
2)響應(yīng)報(bào)文
?
響應(yīng)報(bào)文和請(qǐng)求報(bào)文格式類型,同樣也是分為三個(gè)部分,響應(yīng)頭部和響應(yīng)體同樣也是通過(guò)一個(gè)空行進(jìn)行隔開(kāi),如下為一個(gè)簡(jiǎn)單的示例:
?
HTTP/1.1 200 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>
?
狀態(tài)碼部分
?
該部分主要給出響應(yīng)HTTP協(xié)議版本號(hào)、響應(yīng)返回狀態(tài)碼、響應(yīng)描述,同樣是單行顯示.格式為:
?
HTTP/1.1 200 OK
1
響應(yīng)返回一般由一個(gè)三位數(shù)組成,分別以整數(shù)1~5開(kāi)頭組成。各個(gè)類型響應(yīng)對(duì)應(yīng)含義:
?
1XX:信息提示,臨時(shí)的響應(yīng)
2XX:請(qǐng)求成功,服務(wù)端已經(jīng)接受到請(qǐng)求信息
3XX:重定位
4XX:客戶端異常
5XX:服務(wù)端異常?
常見(jiàn)的響應(yīng)有:200(響應(yīng)成功),400(請(qǐng)求異常,一般為參數(shù)異常),404(請(qǐng)求資源不存在),405(請(qǐng)求方式不支持),500(服務(wù)器內(nèi)部異常)
響應(yīng)頭部
?
響應(yīng)頭部主要是返回一些服務(wù)器的基本信息,以及一些Cookie值等。如上的響應(yīng)頭為:
?
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
?
響應(yīng)體
?
該部分為請(qǐng)求需要得到的具體數(shù)據(jù),可以為任何類型數(shù)據(jù),一般網(wǎng)頁(yè)瀏覽返回的為html文件內(nèi)容.如上,內(nèi)容不再列出。同樣,和響應(yīng)頭部通過(guò)一個(gè)空行進(jìn)行隔開(kāi)。
?
文件上傳報(bào)文格式
?
如下,介紹通過(guò)瀏覽器上傳文件時(shí)它的報(bào)文格式和一般的報(bào)文的異同。文件上傳主要在于文件上傳時(shí)的格式差異,如下給出一個(gè)示例的格式:
?
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="1710600212@qq.com"; 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--
?
如上,我上傳了一個(gè)測(cè)試文件(test.txt),其文件內(nèi)容為:
?
This is the upload file
this is a test file
this is a text file
?
其基本格式還是遵循HTTP的請(qǐng)求格式,但對(duì)于正文部分添加了一些對(duì)于上傳文件的說(shuō)明,格式如下:
?
POST /servlet/UploadServlet HTTP/1.1 ? ?#請(qǐng)求uri、方法
Host: localhost:8080Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryy4ceMstxB6tXIWBT ? ?#邊界值
......
Accept-Language: zh-CN,zh;q=0.8
?
------WebKitFormBoundaryy4ceMstxB6tXIWBT ? ? ? ? ? ? ? ? ? #邊界
Content-Disposition: form-data;name="filedata";filename="test.txt" #文件基本信息
Content-Type: text/plain ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#文件基本信息 ? ? ? ? ??
?
This is the upload file ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #文件內(nèi)容 ? ?
this is a test file
this is a text file
?
------WebKitFormBoundaryy4ceMstxB6tXIWBT ? ? ? #提交按鈕屬性信息部分
Content-Disposition: form-data; name="submit"
?
upload ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?#提交按鈕value值 ? ? ? ? ? ? ? ?
------WebKitFormBoundaryy4ceMstxB6tXIWBT--
?
其他部分和一般的HTTP數(shù)據(jù)格式一樣(包括響應(yīng)數(shù)據(jù)的格式)。
總結(jié)
以上是生活随笔為你收集整理的HTTP 协议报文解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 内容分发网络(CDN) 是什么
- 下一篇: apache sgoop 导入数据到