請求與響應:顧名思義客戶端(瀏覽器)向服務器(web站點)發送請求,並接收服務器發回的響應信息。
客戶端和服務器端都需要有一定的規范、規則,這就是HTTP協議。
一、HTTP協議
①、版本
1.0
1.0
請求數據,服務器返回后, 將會斷開連接
1.1
1.1
請求數據,服務器返回后, 連接還會保持着。 除非服務器 | 客戶端 關掉。 有一定的時間限制,如果都空着這個連接,那么后面會自己斷掉。
②、Http請求數據
包含三個部分內容 : 請求行 、 請求頭 、請求體
格式:
POST /examples/servlets/servlet/RequestParamExample HTTP/1.1 Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */* Referer: http://localhost:8080/examples/servlets/servlet/RequestParamExample Accept-Language: zh-CN User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E) Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate Host: localhost:8080 Content-Length: 31 Connection: Keep-Alive Cache-Control: no-cache firstname=zhang&lastname=sansan
* 請求行 POST /examples/servlets/servlet/RequestParamExample HTTP/1.1
解釋:
POST :
請求方式 ,以post去提交數據
請求方式有兩種
1、post方式
1.1、數據是以流的方式寫過去,不會在地址欄上面顯示。 現在一般提交數據到服務器使用的都是POST
1.2、以流的方式寫數據,所以數據沒有大小限制。
2、get方式
2.1、會在地址欄后面拼接數據,所以有安全隱患。 一般從服務器獲取數據,並且客戶端也不用提交上面數據的時候,可以使用GET
2.2、能夠帶的數據有限, 1kb大小
/examples/servlets/servlet/RequestParamExample 請求的地址路徑 , 就是要訪問哪個地方。
HTTP/1.1 協議版本
/examples/servlets/servlet/RequestParamExample 請求的地址路徑 , 就是要訪問哪個地方。
HTTP/1.1 協議版本
* 請求頭
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Referer: http://localhost:8080/examples/servlets/servlet/RequestParamExample
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:8080
Content-Length: 31
Connection: Keep-Alive
Cache-Control: no-cache
Referer: http://localhost:8080/examples/servlets/servlet/RequestParamExample
Accept-Language: zh-CN
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:8080
Content-Length: 31
Connection: Keep-Alive
Cache-Control: no-cache
解釋:
Accept: 客戶端向服務器端表示,我能支持什么類型的數據。
Referer : 真正請求的地址路徑,全路徑
Accept-Language: 支持語言格式
User-Agent: 用戶代理 向服務器表明,當前來訪的客戶端信息。
Content-Type: 提交的數據類型。經過urlencoding編碼的form表單的數據
Accept-Encoding: gzip, deflate : 壓縮算法 。
Host : 主機地址
Content-Length: 數據長度
Connection : Keep-Alive 保持連接
Cache-Control : 對緩存的操作
Referer : 真正請求的地址路徑,全路徑
Accept-Language: 支持語言格式
User-Agent: 用戶代理 向服務器表明,當前來訪的客戶端信息。
Content-Type: 提交的數據類型。經過urlencoding編碼的form表單的數據
Accept-Encoding: gzip, deflate : 壓縮算法 。
Host : 主機地址
Content-Length: 數據長度
Connection : Keep-Alive 保持連接
Cache-Control : 對緩存的操作
* 請求體:瀏覽器真正發送給服務器的數據
發送的數據呈現的是key=value ,如果存在多個數據,那么使用 &
firstname=zhang&lastname=sansan
③、Http響應數據
包含三個部分內容 : 響應行 、 響應頭 、響應體
格式:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 673
Date: Fri, 17 Feb 2017 02:53:02 GMT
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 673
Date: Fri, 17 Feb 2017 02:53:02 GMT
...這里還有很多數據...
分析:
HTTP/1.1 200 OK(對應前面的狀態碼)
協議版本
狀態碼
200 : 成功,正常處理,得到數據
403 : for bidden 拒絕
404 : Not Found
500 : 服務器異常
403 : for bidden 拒絕
404 : Not Found
500 : 服務器異常
* 響應頭
Server: 服務器是哪一種類型。 Tomcat
Content-Type : 服務器返回給客戶端你的內容類型
Content-Length : 返回的數據長度
Date : 通訊的日期,響應的時間