1. 程式人生 > >http從請求到響應分析

http從請求到響應分析

http協議是網際網路開發的基石,其基於請求/響應模型,請求和響應必須成對出現,預設埠是80.下面分別對http的請求和響應分別做說明。
1、前期準備
新建springmvc工程,將一個html檔案放到tomcat的webapp下,其內容j就是一個簡單的表單提交頁面,如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form
action="/user/testHttp1" method="get">
<input type="text" name="username"><br> <input type="password" name="password"><br> <input type="submit" value="提交"><br> </form> </body> </html>

在controller的java檔案中,建立testHttp1方法用於處理表單請求,並進行String字串返回。只有當用戶名和密碼都正確時,返回login success資訊,否則返回login fail。

    @RequestMapping("/user/testHttp1")
    @ResponseBody
    private String testHttp1(Model model,String username,String password) throws JsonProcessingException{       
        logger.warn("查詢使用者id");
        String result=null;

        if (username.equalsIgnoreCase("andy")&&password.equalsIgnoreCase("123"
)) { result = ("login success"); }else{ result =("login fail"); } return result; }

先以get方式進行提交,通過httpwatch進行抓包檢視。
首先登陸http://localhost:8083/views/test2.html,登陸表單頁面,看其http請求和響應資訊。

GET /views/test2.html HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
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; InfoPath.2)
Accept-Encoding: gzip, deflate
If-Modified-Since: Mon, 25 Jun 2018 08:30:35 GMT
Host: localhost:8083
Connection: Keep-Alive

上面就是http請求,其中:
1、第一行就是請求行,請求頭主要包括請求的方法(即常用的post、get以及其他),資源的地址,以及http的協議版本。
2、從第二行到最後是請求頭,請求頭的內容都是鍵值對,用於向伺服器描述瀏覽器的相關資訊。
比如:Accept:表示瀏覽器能接收的資料型別。
User-Agent:瀏覽器和作業系統的資訊,是ie瀏覽器還是狐火啥的
If-Modified-Since:本地快取的最後變更時間
還有Cookie等相關資訊,這裡沒體現出來。
3、如果是post請求,表單的引數資訊就放在請求體中。此處get方法中,沒有請求體
http響應:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Last-Modified: Mon, 25 Jun 2018 09:05:14 GMT
Accept-Ranges: bytes
Content-Type: text/html
Content-Length: 342
Date: Mon, 25 Jun 2018 09:05:54 GMT

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/user/testHttp1" method="get">
        <input type="text" name="username"><br>
        <input type="password" name="password"><br>
        <input type="submit" value="提交"><br>
          xxx

    </form>

</body>
</html>

http響應的第一行:響應行:包括http協議版本,和狀態碼。以及狀態資訊。
狀態碼的幾個重要數字:
200: 表明請求成功;
302:表明重定向
304:表明瀏覽器本地有快取,訪問本地快取。
404:表明訪問的資源不存在
500:表明伺服器端的程式碼由錯誤

響應頭的資訊
Server: Apache-Coyote/1.1 伺服器的資訊
Last-Modified: Mon, 25 Jun 2018 09:05:14 GMT :伺服器關於此資源的最後更改時間。和請求頭的If-Modified-Since的時間來去確定是否訪問本地快取
Accept-Ranges: bytes
Content-Type: text/html :返回的內容格式
Content-Length: 342
Date: Mon, 25 Jun 2018 09:05:54 GMT

響應體的資訊就是瀏覽器呈現給使用者的資訊。

在表單中輸入使用者名稱和密碼,點選提交,然後看其http請求和響應:

POST /user/testHttp1 HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://localhost:8083/views/test2.html
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; InfoPath.2)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: localhost:8083
Content-Length: 26
Connection: Keep-Alive
Cache-Control: no-cache

username=andy&password=123

以及響應

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/x-ms-application
Content-Length: 13
Date: Mon, 25 Jun 2018 10:00:05 GMT

login success

在post請求中,http請求體包含了表單的引數,因此post請求比get請求要安全。