图解 HTTP - [日] 上野宣

本书对互联网基盘——HTTP 协议进行了全面系统的介绍。作者由 HTTP 协议的发展史娓娓道来,严谨细致地剖析了 HTTP 协议的结构,列举诸多常见通信场景及实战案例。本书的特色为在讲解的同时,辅以大量生动形象的通信图例,更好地帮助读者深刻理解 HTTP 通信过程中客户端与服务器之间的交互情况。
关于作者
上野宣 是日本知名的网络技术专家和教育者:
- 网络技术顾问:专注于 Web 技术和网络协议的研究与咨询
- 技术作家:著有多本网络技术相关的入门和进阶书籍
- 教育家:擅长用通俗易懂的方式讲解复杂的技术概念
上野宣以其"图解"系列的写作风格著称,通过大量直观的图表帮助读者理解抽象的网络协议概念。
核心内容
1. HTTP 基础
HTTP 请求报文格式:
请求行:GET /index.html HTTP/1.1
请求头:
Host: www.example.com
User-Agent: Mozilla/5.0
Accept: text/html,application/xhtml+xml
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
请求体:(POST 请求时包含数据)
HTTP 响应报文格式:
状态行:HTTP/1.1 200 OK
响应头:
Server: nginx
Content-Type: text/html; charset=UTF-8
Content-Length: 1234
Set-Cookie: sessionId=abc123; Path=/; HttpOnly
响应体:<html>...</html>
2. HTTP 方法
// GET - 获取资源
GET /api/users/123 HTTP/1.1
Host: api.example.com
// POST - 创建资源
POST /api/users HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
"name": "Alice",
"email": "alice@example.com"
}
// PUT - 更新资源(全量)
PUT /api/users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
"name": "Alice Updated",
"email": "alice.updated@example.com"
}
// PATCH - 更新资源(部分)
PATCH /api/users/123 HTTP/1.1
Host: api.example.com
Content-Type: application/json
{
"name": "Alice New"
}
// DELETE - 删除资源
DELETE /api/users/123 HTTP/1.1
Host: api.example.com
3. 状态码
1xx - 信息性状态码
100 Continue - 继续发送请求体
2xx - 成功状态码
200 OK - 请求成功
201 Created - 资源创建成功
204 No Content - 成功但无返回内容
3xx - 重定向状态码
301 Moved Permanently - 永久重定向
302 Found - 临时重定向
304 Not Modified - 资源未修改(缓存)
4xx - 客户端错误
400 Bad Request - 请求语法错误
401 Unauthorized - 需要认证
403 Forbidden - 禁止访问
404 Not Found - 资源不存在
429 Too Many Requests - 请求过于频繁
5xx - 服务器错误
500 Internal Server Error - 服务器内部错误
502 Bad Gateway - 网关错误
503 Service Unavailable - 服务不可用
504 Gateway Timeout - 网关超时
4. HTTP 首部字段
// 通用首部
Connection: keep-alive
Date: Mon, 23 May 2022 12:00:00 GMT
Cache-Control: no-cache, no-store, must-revalidate
// 请求首部
Accept: application/json
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
User-Agent: Mozilla/5.0...
Authorization: Bearer eyJhbGciOiJIUzI1NiIs...
// 响应首部
Content-Type: application/json; charset=utf-8
Content-Length: 1234
Set-Cookie: sessionId=abc123; HttpOnly; Secure
Access-Control-Allow-Origin: *
// 实体首部
Content-Encoding: gzip
Content-Range: bytes 0-1023/2048
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
Last-Modified: Wed, 21 Oct 2015 07:28:00 GMT