HTTP 協議字段與狀態碼完整指南
一、HTTP 字段(請求頭與響應頭)
HTTP 頭字段用于傳遞客戶端和服務器之間的元數據,分為 請求頭(Request Headers) 和 響應頭(Response Headers)。
1. 常見請求頭字段
字段名 | 說明 | 示例值 |
---|---|---|
Host | 目標服務器的主機名和端口號(必填字段)。 | Host: example.com:8080 |
User-Agent | 客戶端標識(瀏覽器、操作系統或應用程序信息)。 | User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) |
Accept | 客戶端支持的響應內容類型(MIME 類型)。 | Accept: application/json, text/html |
Accept-Encoding | 支持的壓縮編碼。 | Accept-Encoding: gzip, deflate |
Content-Type | 請求體的媒體類型。 | Content-Type: application/json |
Authorization | 身份憑證(如 JWT Token)。 | Authorization: Bearer eyJhbGciOiJ... |
Cookie | 客戶端發送的 Cookie 信息。 | Cookie: session_id=abc123; user=admin |
Cache-Control | 緩存策略。 | Cache-Control: no-cache (禁用緩存) |
Referer | 當前請求的來源頁面 URL。 | Referer: https://google.com |
Accept-Charset | 聲明客戶端支持的字符集及其優先級(如 UTF-8、ISO-8859)。現代瀏覽器通常默認支持 UTF-8,此字段已較少顯式使用。 | Accept-Charset: utf-8, iso-8859-1; q=0.5 (q 值表示權重) |
Accept-Language | 指定客戶端期望的自然語言(如中文、英文)。服務端根據優先級返回多語言內容。 | Accept-Language: zh-CN, en-US; q=0.8 |
Cookie | 客戶端向服務器發送已存儲的 Cookie 數據。 | Cookie: session_id=abc123; user=admin |
If-Modified-Since | 資源在指定時間后未修改則返回 304 狀態碼,使用本地緩存。關聯字段:Last-Modified (響應頭)。 | If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT |
If-None-Match | 攜帶資源的 ETag 值,若未變化則返回 304。比時間戳更精確,避免時間同步問題。 | If-None-Match: "737060cd8c284d8af7ad3082f209582d" |
X-Forwarded-For | 標識客戶端的原始 IP(常用于代理鏈中記錄真實來源)。非標準字段,需代理服務器顯式添加。 | X-Forwarded-For: 192.168.1.1, 10.0.0.1 (第一個 IP 為客戶端) |
Priority | 提示服務器請求的優先級(如資源加載順序)。優化頁面加載性能,需瀏覽器和服務端共同支持(當前支持度有限)。在 HTTP/2 和 HTTP/3 中,優先級機制通過專門的?PRIORITY_UPDATE ?幀實現,而非頭字段。 | Priority: u=1, i (表示高優先級) |
2. 常見響應頭字段
字段名 | 說明 | 示例值 |
---|---|---|
Content-Type | 響應體的媒體類型及編碼。 | Content-Type: text/html; charset=UTF-8 |
Content-Encoding | 響應體的壓縮方式。 | Content-Encoding: gzip |
Server | 服務器軟件信息。 | Server: Apache/2.4.1 |
Set-Cookie | 服務器設置的 Cookie。 | Set-Cookie: session_id=def456; Path=/; HttpOnly |
Location | 重定向目標 URL(用于 3xx 狀態碼)。 | Location: https://new-example.com |
Cache-Control | 資源緩存策略。 | Cache-Control: public, max-age=3600 (緩存 1 小時) |
Access-Control-Allow-Origin | 允許跨域請求的源。 | Access-Control-Allow-Origin: * (允許所有域名) |
ETag | 資源的唯一標識符(用于緩存驗證)。 | ETag: "12345abcde" |
WWW-Authenticate | 定義身份驗證方式(如 Basic、Bearer Token)。配合 401 狀態碼要求客戶端提供憑證。 | WWW-Authenticate: Basic realm="Access to site" |
Last-Modified | 資源最后修改時間,用于緩存驗證。關聯請求頭:If-Modified-Since 。 | Last-Modified: Tue, 15 Nov 1994 08:12:31 GMT |
Expires | 指定資源過期時間(HTTP/1.0 緩存機制)。已被 Cache-Control 的 max-age 取代,優先級更低。 | Expires: Thu, 01 Dec 2025 16:00:00 GMT |
3. 通用字段
字段名 | 說明 | 示例值 |
---|---|---|
Connection | 控制連接是否在當前事務完成后關閉。優化性能時啟用長連接,減少重復握手開銷。 | Connection: keep-alive (HTTP/1.1 默認,保持長連接)Connection: close (HTTP/1.0 默認,關閉連接) |
Content-Length | 表示請求或響應體的字節長度。若使用分塊傳輸(Transfer-Encoding: chunked ),則無需此字段。 | Content-Length: 348 |
Date | 報文創建時間,格式為格林威治時間(GMT)。用于緩存驗證和日志記錄。 | Date: Tue, 15 Nov 1994 08:12:31 GMT |
4. 請求頭與響應頭完整示例
請求示例
GET /api/user?id=123 HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)
Accept: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Cache-Control: no-cache
Cookie: session_id=abc123
響應示例
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
Cache-Control: max-age=3600
Set-Cookie: session_id=def456; Path=/; HttpOnly
ETag: "12345abcde"
Content-Length: 128{"id": 123, "name": "John Doe"}
二、HTTP 狀態碼詳解
HTTP 狀態碼由三位數字組成,分為五類:
1. 1xx(信息響應)
- 100 Continue
客戶端應繼續發送請求體(用于大文件上傳前的確認)。 - 101 Switching Protocols
服務器同意切換協議(如從 HTTP 切換到 WebSocket)。
2. 2xx(成功)
- 200 OK
請求成功,返回預期結果。
HTTP/1.1 200 OK
Content-Type: text/html
<html>...</html>
- 201 Created
資源已創建(常用于 POST 請求)。
HTTP/1.1 201 Created
Location: /api/users/789
{"id": 789, "message": "User created"}
- 204 No Content
請求成功,但無返回內容(如 DELETE 請求)。
3. 3xx(重定向)
- 301 Moved Permanently
資源永久遷移到新 URL(SEO 友好)。
HTTP/1.1 301 Moved Permanently
Location: https://new-example.com
- 302 Found
資源臨時重定向(瀏覽器下次仍請求原 URL)。
HTTP/1.1 302 Found
Location: /temp-page
- 304 Not Modified
資源未修改,客戶端使用本地緩存。
HTTP/1.1 304 Not Modified
ETag: "12345abcde"
4. 4xx(客戶端錯誤)
- 400 Bad Request
請求格式錯誤(如缺少必填字段)。
HTTP/1.1 400 Bad Request
{"error": "Missing 'email' field"}
- 401 Unauthorized
需要身份驗證(未提供有效憑證)。
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Bearer realm="example.com"
- 403 Forbidden
服務器拒絕訪問(權限不足)。
HTTP/1.1 403 Forbidden
{"error": "Access denied"}
- 404 Not Found
請求的資源不存在。
HTTP/1.1 404 Not Found
<h1>404 Page Not Found</h1>
5. 5xx(服務器錯誤)
- 500 Internal Server Error
服務器內部錯誤(通用錯誤碼)。
HTTP/1.1 500 Internal Server Error
{"error": "Database connection failed"}
- 502 Bad Gateway
代理服務器從上游服務器收到無效響應。
HTTP/1.1 502 Bad Gateway
- 503 Service Unavailable
服務器暫時不可用(維護或過載)。
HTTP/1.1 503 Service Unavailable
Retry-After: 3600
三、關鍵總結與最佳實踐
-
HTTP 字段
- 使用
Cache-Control
和ETag
優化緩存性能。 - 跨域請求時配置
Access-Control-Allow-Origin
。 - 敏感信息通過
Authorization
頭傳遞,而非 URL 參數。
- 使用
-
狀態碼使用原則
- 精準匹配場景:避免濫用
200
或500
。- 資源不存在 →
404
- 身份未認證 →
401
- 權限不足 →
403
- 資源不存在 →
- 重定向區分:
- 永久遷移 →
301
- 臨時跳轉 →
302
- 永久遷移 →
- 客戶端錯誤:提供清晰的錯誤描述(如
400
時指明缺失字段)。
- 精準匹配場景:避免濫用
-
安全建議
- Cookie 設置
HttpOnly
和Secure
屬性。 - 使用 HTTPS 加密傳輸敏感數據。
- Cookie 設置
四、特殊狀態碼(彩蛋)
- 418 I’m a Teapot
HTTP/1.1 418 I'm a Teapot
Content-Type: text/plain
I'm a teapot. Can't brew coffee.
源自 RFC 2324,用于幽默或測試場景。
附錄:完整 HTTP 狀態碼列表可參考 MDN Web Docs。