1.HTTP协议由哪些部分组成?可以使用抓包工具去查看和理解
HTTP 请求的组成
状态行、请求头、消息主体三部分组成。
HTTP 响应的组成
状态行、响应头、响应正文
① 在命令行下查看HTTP协议,可以使用 curl 或 http 命令发起HTTP请求
[root@localhost ~]# curl www.baidu.com # 使用curl发起HTTP请求
② 使用http 命令需要首先安装httpie,然后在通过http命令发起HTTP请求
[root@localhost ~]# pip install httpie # 安装httpie
[root@localhost ~]# http baidu.com # 使用http发起HTTP请求
查看HTTP协议详细输出过程, http -v 详细输出过程
[root@localhost ~]# http -v baidu.com
GET / HTTP/1.1 # 状态行包括请求方法、路径、http版本
Accept: */* # 3~7行是http的请求头
Accept-Encoding: gzip, deflate # 接收的编码
Connection: keep-alive # 连接,keep-alive表示长连接
Host: baidu.com # 域名
User-Agent: HTTPie/1.0.2 # 请求的代理
# 消息主体为空,这里没有显示,一般是由GET请求的时候它就没有消息主体
-------------------------------------------------------------------------------------
HTTP/1.1 200 OK # 下面是HTTP响应
Accept-Ranges: bytes
Cache-Control: max-age=86400
Connection: Keep-Alive
Content-Length: 81
Content-Type: text/html
Date: Fri, 02 Aug 2019 10:07:15 GMT
ETag: "51-47cf7e6ee8400"
Expires: Sat, 03 Aug 2019 10:07:15 GMT
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
Server: Apache
<html>
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>
提交一些表单的时候HTTP请求里面就会包含消息主体。
发送一个POST请求,带上hello=World参数
[root@localhost ~]# http -f POST baidu.com hello=World -v
POST / HTTP/1.1 # 请求状态行
Accept: */* # 3~9是请求头
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 11
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Host: baidu.com
User-Agent: HTTPie/1.0.2
hello=World # 消息主题,当然有时候可以为空
------------------------------------------------------------------------------
HTTP/1.1 200 OK # 响应状态行,包括HTTP版本、状态码200、OK表示成功
Accept-Ranges: bytes # 14~23是响应头
Cache-Control: max-age=86400
Connection: Keep-Alive
Content-Length: 81
Content-Type: text/html
Date: Fri, 02 Aug 2019 10:13:42 GMT
ETag: "51-47cf7e6ee8400"
Expires: Sat, 03 Aug 2019 10:13:42 GMT
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
Server: Apache
<html> # 响应正文。返回的是一个HTML
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
</html>
一般是由GET请求的时候它就没有消息主体