Servlet&HTTP8_HTTP2
1、概念
Hyper Text Transfer Protocol 超文本传输协议
- 传输协议:定义了客户端和服务器端通信时,发送数据的格式
- 特点:
- 基于TCP/IP的高级协议
- 默认端口号:80。例如:http://www.baidu.com:80
- 基于请求/响应模型的:一次请求对应一次响应
- 无状态的:每次请求之间相互独立,不能交互数据
- 历史版本:
- 1.0:每一次请求响应都会建立新的连接
- 1.1:复用连接
2、请求消息数据格式
* 字符串格式:
GET /login.html HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
sec-ch-ua: " Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: none
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cookie: JSESSIONID=A0B29B942241CE2906EDDBE0B29832CC
If-None-Match: W/"264-1644755579340"
If-Modified-Since: Sun, 13 Feb 2022 12:32:59 GMT
POST /demo3 HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 12
Cache-Control: max-age=0
sec-ch-ua: " Not;A Brand";v="99", "Google Chrome";v="97", "Chromium";v="97"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
Upgrade-Insecure-Requests: 1
Origin: http://localhost
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Referer: http://localhost/login.html
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cookie: JSESSIONID=A0B29B942241CE2906EDDBE0B29832CC
username=aaa
1. 请求行
* 格式:请求方式 请求url 请求协议/版本
GET /login.html HTTP/1.1
* 请求方式:HTTP协议有7种请求方式,常用的有2种
a. GET:
1. 请求参数在请求行中,在url后
2. 请求的url长度有限制的
3. 不太安全
b. POST:
1. 请求参数在请求体中
2. 请求的url长度没有限制的
3. 相对安全
2. 请求头:客户端浏览器告诉服务器一些信息
* 格式:请求头名称: 请求头值
* 常见的请求头:
a. User-Agent:浏览器告诉服务器,我访问你使用的浏览器版本信息
可以在服务器端获取该头的信息,解决浏览器的兼容性问题
b. Referer:http://localhost/login.html,告诉服务器,我(当前请求)从哪里来。
作用:防盗链,统计工作。
3. 请求空行
空行,就是用于分割POST请求的请求头和请求体的。
4. 请求体(正文):封装POST请求消息的请求参数的。GET方式没有请求体。
3、响应消息数据格式