|
1 | 1 | <!-- GFM-TOC -->
|
2 | 2 | * [一 、基础概念](#一-基础概念)
|
3 |
| - * [URI](#uri) |
4 | 3 | * [请求和响应报文](#请求和响应报文)
|
| 4 | + * [URL](#url) |
5 | 5 | * [二、HTTP 方法](#二http-方法)
|
6 | 6 | * [GET](#get)
|
7 | 7 | * [HEAD](#head)
|
|
58 | 58 |
|
59 | 59 | # 一 、基础概念
|
60 | 60 |
|
61 |
| -## URI |
| 61 | +## 请求和响应报文 |
62 | 62 |
|
63 |
| -URI 包含 URL 和 URN。 |
| 63 | +客户端发送一个请求报文给服务器,服务器根据请求报文中的信息进行处理,并将处理结果放入响应报文中返回给客户端。 |
64 | 64 |
|
65 |
| -<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/8441b2c4-dca7-4d6b-8efb-f22efccaf331.png" width="500px"> </div><br> |
| 65 | +请求报文结构: |
66 | 66 |
|
67 |
| -## 请求和响应报文 |
| 67 | +- 第一行是包含了请求方法、URL、协议版本; |
| 68 | +- 接下来的多行都是请求首部 Header,每个首部都有一个首部名称,以及对应的值。 |
| 69 | +- 一个空行用来分隔首部和内容主体 Body |
| 70 | +- 最后是请求的内容主体 |
| 71 | + |
| 72 | +``` |
| 73 | +GET http://www.example.com/ HTTP/1.1 |
| 74 | +Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 |
| 75 | +Accept-Encoding: gzip, deflate |
| 76 | +Accept-Language: zh-CN,zh;q=0.9,en;q=0.8 |
| 77 | +Cache-Control: max-age=0 |
| 78 | +Host: www.example.com |
| 79 | +If-Modified-Since: Thu, 17 Oct 2019 07:18:26 GMT |
| 80 | +If-None-Match: "3147526947+gzip" |
| 81 | +Proxy-Connection: keep-alive |
| 82 | +Upgrade-Insecure-Requests: 1 |
| 83 | +User-Agent: Mozilla/5.0 xxx |
| 84 | +
|
| 85 | +param1=1¶m2=2 |
| 86 | +``` |
| 87 | + |
| 88 | +响应报文结构: |
68 | 89 |
|
69 |
| -### 1. 请求报文 |
| 90 | +- 第一行包含协议版本、状态码以及描述,最常见的是 200 OK 表示请求成功了 |
| 91 | +- 接下来多行也是首部内容 |
| 92 | +- 一个空行分隔首部和内容主体 |
| 93 | +- 最后是响应的内容主体 |
70 | 94 |
|
71 |
| -<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/HTTP_RequestMessageExample.png" width=""/> </div><br> |
| 95 | +``` |
| 96 | +HTTP/1.1 200 OK |
| 97 | +Age: 529651 |
| 98 | +Cache-Control: max-age=604800 |
| 99 | +Connection: keep-alive |
| 100 | +Content-Encoding: gzip |
| 101 | +Content-Length: 648 |
| 102 | +Content-Type: text/html; charset=UTF-8 |
| 103 | +Date: Mon, 02 Nov 2020 17:53:39 GMT |
| 104 | +Etag: "3147526947+ident+gzip" |
| 105 | +Expires: Mon, 09 Nov 2020 17:53:39 GMT |
| 106 | +Keep-Alive: timeout=4 |
| 107 | +Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT |
| 108 | +Proxy-Connection: keep-alive |
| 109 | +Server: ECS (sjc/16DF) |
| 110 | +Vary: Accept-Encoding |
| 111 | +X-Cache: HIT |
| 112 | +
|
| 113 | +<!doctype html> |
| 114 | +<html> |
| 115 | +<head> |
| 116 | + <title>Example Domain</title> |
| 117 | + // 省略... |
| 118 | +</body> |
| 119 | +</html> |
72 | 120 |
|
73 |
| -### 2. 响应报文 |
| 121 | +``` |
74 | 122 |
|
75 |
| -<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/HTTP_ResponseMessageExample.png" width=""/> </div><br> |
| 123 | +## URL |
| 124 | + |
| 125 | +http 使用 URL( **U** niform **R**esource **L**ocator,统一资源定位符)来定位资源,它可以认为是是 URI(**U**niform **R**esource **I**dentifier,统一资源标识符)的一个子集,URL 在 URI 的基础上增加了定位能力。URI 除了包含 URL 之外,还包含 URN(Uniform Resource Name,统一资源名称),它知识用来定义一个资源的名称,并不具备定位该资源的能力。例如 urn:isbn:0451450523 用来定义一个书籍,但是却没有表示怎么找到这本书。 |
| 126 | + |
| 127 | +<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/8441b2c4-dca7-4d6b-8efb-f22efccaf331.png" width="500px"> </div><br> |
| 128 | + |
| 129 | +- [wikipedia:统一资源标志符](https://zh.wikipedia.org/wiki/统一资源标志符) |
| 130 | +- [wikipedia: URL](https://en.wikipedia.org/wiki/URL) |
| 131 | +- [rfc2616:3.2.2 http URL](https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.2.2) |
| 132 | +- [What is the difference between a URI, a URL and a URN?](https://stackoverflow.com/questions/176264/what-is-the-difference-between-a-uri-a-url-and-a-urn) |
76 | 133 |
|
77 | 134 | # 二、HTTP 方法
|
78 | 135 |
|
@@ -171,6 +228,8 @@ CONNECT www.example.com:443 HTTP/1.1
|
171 | 228 |
|
172 | 229 | 通常不会使用 TRACE,并且它容易受到 XST 攻击(Cross-Site Tracing,跨站追踪)。
|
173 | 230 |
|
| 231 | +- [rfc2616:9 Method Definitions](https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html) |
| 232 | + |
174 | 233 | # 三、HTTP 状态码
|
175 | 234 |
|
176 | 235 | 服务器返回的 **响应报文** 中第一行为状态行,包含了状态码以及原因短语,用来告知客户端请求的结果。
|
|
0 commit comments