HTTP作用:指导浏览器与服务器进行沟通
- 浏览器负责发起请求
- 服务器在 80 端口接收请求
- 服务器负责返回内容(响应)
- 浏览器负责下载响应内容
使用Chrome开发工具查看请求与响应
①Ctrl + Shift + I / 右键-->检查
②在 Network 点击,查看 request,点击「view source」
1.请求
请求的格式
1 动词 路径 协议/版本
2 Key1: value1
2 Key2: value2
2 Key3: value3
2 Content-Type: application/x-www-form-urlencoded
2 Host: www.baidu.com
2 User-Agent: curl/7.54.0
3
4 要上传的数据
- 请求最多包含四部分,最少包含三部分。(也就是说第四部分可以为空)
- 第三部分永远都是一个回车(\n)
- 动词有 GET POST PUT PATCH DELETE HEAD OPTIONS 等
- 这里的路径包括「查询参数」,但不包括「锚点」
- 如果你没有写路径,那么路径默认为 /
- 第 2 部分中的 Content-Type 标注了第 4 部分的格式
补充:HTTP请求方法对照表
2.响应
响应的格式
1 协议/版本号 状态码 状态解释
2 Key1: value1
2 Key2: value2
2 Content-Length: 17931
2 Content-Type: text/html
3
4 要下载的内容
- 状态码是服务器对浏览器说的话
HTTP状态码对照表 - 第 2 部分中的 Content-Type 标注了第 4 部分的格式
- 第 2 部分中的 Content-Type 遵循 MIME 规范
使用curl查看请求与响应
curl -s -v -- "https://www.baidu.com"
请求的内容:
GET / HTTP/1.1
Host: www.baidu.com
User-Agent: curl/7.55.0
Accept: /
响应的内容:
HTTP/1.1 200 Ok
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: Keep-Alive
Content-Length: 2443
Content-Type: text/html
Date: Thu, 07 Feb 2019 14:41:27 GMT
Etag: "588603e6-98b"
Last-Modified: Mon, 23 Jan 2017 13:23:50 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
curl -X POST -d "123456" -s -v -- "https://www.baidu.com"
请求的内容:
POST / HTTP/1.1
Host: www.baidu.com
User-Agent: curl/7.55.0
Accept: /
Content-Length: 6
Content-Type: application/x-www-form-urlencoded
响应的内容:
HTTP/1.1 302 Found
Connection: Keep-Alive
Content-Length: 17931
Content-Type: text/html
Date: Thu, 07 Feb 2019 14:42:57 GMT
Etag: "54d97485-460b"
Server: bfe/1.0.8.18
网友评论