http数据包

Ni5h1hundun / 2023-05-04 / 原文

http数据包

HTTP 由请求和响应两部分组成,所以对应的也有两种报文格式。下面分别介绍 HTTP
请求报文格式和 HTTP 响应报文格式

手边正好有抓的一个包(buu的)

请求头
POST /Login HTTP/1.1
Host: 79fd8c0f-490b-4f57-9847-41eccb7bf59d.node4.buuoj.cn:81
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: http://79fd8c0f-490b-4f57-9847-41eccb7bf59d.node4.buuoj.cn:81/
Content-Type: application/x-www-form-urlencoded
Content-Length: 27
Origin: http://79fd8c0f-490b-4f57-9847-41eccb7bf59d.node4.buuoj.cn:81
Connection: close
Cookie: JSESSIONID=9BB6C3275286DFB42088CC16FFB1A790
Upgrade-Insecure-Requests: 1

username=admin&password=123

下面的是响应

HTTP/1.1 200 OK
Server: openresty
Date: Fri, 28 Apr 2023 14:36:31 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 75
Connection: close

<h1>wrong password!</h1><h3><a href="Login">Click to log in again!</a></h3>

http请求包

HTTP请求报文由请求行、请求首部和主体三部分组成。格式如下:

请求行 
请求首部1: 值1
请求首部2: 值2
...
请求首部n: 值n

主体(可选)
  1. 请求行:由请求方法、请求URI和HTTP版本三部分组成。
  • 请求方法:GET、POST、PUT、DELETE等,用于描述对资源的操作
  • 请求URI:包含协议名、主机名和资源路径,用于标识请求资源
  • HTTP版本:通常为HTTP/1.1
    例如:GET /index.html HTTP/1.1
  1. 请求首部:包含多个首部字段,描述请求属性和主体信息。常见首部有:
  • Accept:用户代理可接受的媒体类型
  • Accept-Encoding:用户代理可接受的内容编码
  • User-Agent:用户代理的信息
  • Host:请求资源所在服务器
  • Content-Type:主体内容类型
  • Content-Length:主体内容长度
  • Cookie:页面访问会话的状态信息
    例如:Accept: text/html; User-Agent: Chrome/68
  1. 主体:可选,包含请求所携带的数据信息,在POST/PUT请求中会包含。以Content-Type首部描述的格式编码,长度由Content-Length指定。
    例如:

     Content-Type: application/x-www-form-urlencoded
     username=johndoe&age=30
    

下面我们再解析一下上面的请求头的代码

    /*/ - POST方法:请求登录资源
        - /Login:请求的登录接口路径
        - HTTP/1.1:HTTP版本号
    /*/
    POST /Login HTTP/1.1
        Host: 79fd8c0f-490b-4f57-9847-41eccb7bf59d.node4.buuoj.cn:81

        User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0

        Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8

        Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2

        Accept-Encoding: gzip, deflate
        
        Referer: http://79fd8c0f-490b-4f57-9847-41eccb7bf59d.node4.buuoj.cn:81/

        Content-Type: application/x-www-form-urlencoded

        Content-Length: 27

        Origin: http://79fd8c0f-490b-4f57-9847-41eccb7bf59d.node4.buuoj.cn:81

        Connection: close

        Cookie: JSESSIONID=9BB6C3275286DFB42088CC16FFB1A790

        Upgrade-Insecure-Requests: 1
        /*/
        - Host:请求资源所在主机,包含域名和端口
        - User-Agent:浏览器版本信息
        - Accept:浏览器可接受的媒体类型 
        - Accept-Language:浏览器可接受的语言 
        - Accept-Encoding:浏览器可接受的编码格式 
        - Referer:请求来源页面的URI
        - Content-Type:请求主体内容类型,application/x-www-form-urlencoded
        - Content-Length:请求主体内容长度,27
        - Origin:请求来源,与referer作用类似
        - Connection:连接类型,close表示断开连接
        - Cookie:会话信息,包含JSESSIONID
        - Upgrade-Insecure-Requests:升级为HTTPS请求
        /*/
        username=admin&password=123

http响应包

HTTP/1.1 200 OK   #HTTP版本号和状态码,200表示成功
Server: openresty     #Web服务器类型
Date: Fri, 28 Apr 2023 14:36:31 GMT   #响应日期
Content-Type: text/html; charset=utf-8   #响应内容类型和字符编码
Content-Length: 75    #响应内容长度
Connection: close      #连接类型,close表示断开连接 
#响应主体:
<h1>wrong password!</h1>     #标题,密码错误
<h3><a href="Login">Click to log in again!</a></h3> 
#提示信息,点击链接再次登录