
图中:
- 浏览器负责发起“请求”。
- 服务器在80端口接受“请求”。
- 服务器负责返回内容(响应)。
- 浏览器负责下载响应的内容。
简单说,HTTP指导浏览器和服务器进行沟通。
HTTP请求:一共可以有4个部分。
第1部分:动词 路径 协议/版本。
第2部分:各种key-value,其中比较典型的是:
Content-Type: application/x-www-form-urlencoded
Host: www.baidu.com
User-Agent: curl/7.54.0
第3部分:回车。
第4部分:要上传的内容。
注意:
- 请求最多包含四个部分,最少包含三个部分(第四个部分可能没有)。
- 第三部分永远是回车(\n)
- 动词包括GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS等。
- 这里的路径包括「查询参数」,但是不含有「锚点」。
- 第2部分的Content-Type标注了第4部分的格式。
用chrome发请求:
- 打开chrome。
- 输入http://www.baidu.com网址。
-
点击Network,查看request, 点击「view source」。
image.png
HTTP响应:一共也是可以由4部分组成。
- 协议/版本号 状态码 状态解释
- 各种key-value,其中比较典型的是:
Content-Length: 17931
Content-Type: text/html
- 回车
- 要下载的内容
注意:
- 状态码,可就是服务器对浏览器说的话。
1xx :不常用
2xx :表示成功
3xx: 表示滚吧
4xx: 表示浏览器错了
5xx: 表示服务器我错了 - 状态解释没有什么用
- 第 2 部分中的 Content-Type 标注了第 4 部分的格式
- 第 2 部分中的 Content-Type 遵循 MIME 规范
用chrome查看响应:
- 打开chrome。
- 输入http://www.baidu.com网址。
-
点击Network,查看response, 点击「view source」。
image.png
请求示例:
curl -v -s -H "Jason:xxx" -- "http://www.baidu.com"

请求内容:
GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.54.0
> Accept: */*
> Jason:xxx
响应示例:
上列请求示例中也有响应内容。
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Wed, 08 Aug 2018 11:12:16 GMT
< Etag: "588604f8-94d"
< Last-Modified: Mon, 23 Jan 2017 13:28:24 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
<!DOCTYPE html>。。。。。。。
网友评论