HTTP 报文组成

2016-12-21  本文已影响45人  刘挚珂

总结一下自己之前的学习笔记

HTTP协议

报文的整体格式

- 报文首部
    - 报文首行
    - 报文头
- 空行
- 报文体

请求报文:

请求首行
请求头
空行
请求体

响应报文:

响应首行
响应头
空行
响应体

get请求报文

GET /test/index.html HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: en-US,zh-CN;q=0.7,ko-KR;q=0.3
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; InfoPath.3; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:8020
Connection: Keep-Alive
http://127.0.0.1:8020/test/index.html
http://www.baidu.com
http://主机地址:端口号

get请求没有请求体,所以也看不到空行, get请求的请求参数是通过URL地址来发送的

请求报文的结构

请求首行

GET  /test/index.html  HTTP/1.1
请求方式  资源路径  协议名/协议版本

请求头:

通过观察发现请求头中的内容都是一个属性名对应属性值的形式,有的是一个属性对应一个值,有的是一个属性对应多个值

Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
浏览器允许的文件的类型:`*/*` 代表任意类型
Accept-Language: en-US,zh-CN;q=0.7,ko-KR;q=0.3
允许语言:en-US表示英语 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; InfoPath.3; .NET4.0C; .NET4.0E)
浏览器的信息:
Accept-Encoding: gzip, deflate
允许的压缩格式:
Host: 127.0.0.1:8020
主机地址: 
Connection: Keep-Alive
连接时间:Keep-Alive告诉服务器等我一会,等我将全部的资源都加载完你在断开连接

post请求报文

    POST /day03/taget.html HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Referer: http://127.0.0.1:8020/day03/1.html
Accept-Language: en-US,zh-CN;q=0.7,ko-KR;q=0.3
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; InfoPath.3; .NET4.0C; .NET4.0E)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: 127.0.0.1:8020
Content-Length: 14
Connection: Keep-Alive
Cache-Control: no-cache
缓存控制:不使用缓存

username=admin

请求首行

    POST /test/taget.html HTTP/1.1
    请求方式 资源地址 协议名/版本

请求头

    Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
    浏览器允许的文件的类型:
    Referer: http://127.0.0.1:8020/test/index.html
    请求的来源:作用:1.广告计费 2.防盗链
    Accept-Language: en-US,zh-CN;q=0.7,ko-KR;q=0.3
    允许语言
    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; InfoPath.3; .NET4.0C; .NET4.0E)
    浏览器的信息:
    Content-Type: application/x-www-form-urlencoded
    Accept-Encoding: gzip, deflate
    Host: 127.0.0.1:8020
    Content-Length: 14
    Connection: Keep-Alive
    Cache-Control: no-cache

空行

空行用来分割请求首部和请求体

请求体

用户发送给服务器的请求参数

响应报文:

HTTP/1.1 200 OK
Date: Sat, 19 Mar 2016 06:23:22 GMT
响应日期: 格林威治标准时间
Server: HttpComponents/4.1.3
服务器版本:
Content-Length: 148
Content-Type: text/html
响应体类型:
Connection: Keep-Alive

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <h1>??‘??ˉ??ü??a??‘é?μ</h1>
    </body>
</html>

响应首行

    HTTP/1.1 200 OK
    协议/版本 响应状态码 响应状态码的描述
    常见的响应状态码:
        200 响应成功 凡是2开头的全都是成功相关
        404 资源未找到 凡是4开头的全都是浏览器的错误
        500 服务器内部错误 凡是5开头的全都是服务器的错
        302 请求的重定向 凡是3开头都和重定向相关

响应头

    Date: Sat, 19 Mar 2016 06:23:22 GMT
    响应的日期:格林威治标准时间
    
    Server: HttpComponents/4.1.3
    Content-Length: 148
    Content-Type: text/html
    响应体的类型:text/html表示请求体中的内容是一个HTML网页
    
    Connection: Keep-Alive
    

空行

分割首部和体

响应体

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
        <body>
            <h1>我是一个网页</h1>
        </body>
    </html>
    

get请求和post请求区别:

1. get请求通过url地址发送请求参数,请求参数可以直接在地址栏看见,不太安全。
    post请求通过请求体来发送请求参数,地址栏不能直接看到,相对安全。
2.除了表单的method属性设置为post的情况,其余全都是get请求
3.一般提交表单时都用post请求
上一篇下一篇

猜你喜欢

热点阅读