- HTTP 教程
- HTTP - 首頁
- HTTP - 概述
- HTTP - 引數
- HTTP - 訊息
- HTTP - 請求
- HTTP - 響應
- HTTP - 方法
- HTTP - 狀態碼
- HTTP - 報頭欄位
- HTTP - 快取
- HTTP - URL 編碼
- HTTP - 安全性
- HTTP - 訊息示例
- HTTP 有用資源
- HTTP - 快速指南
- HTTP - 有用資源
HTTP - 響應
伺服器在接收和解釋請求訊息後,會返回一個 HTTP 響應訊息。
- A Status-line
- Zero or more header (General|Response|Entity) fields followed by CRLF
- An empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields
- Optionally a message-body
以下部分解釋了 HTTP 響應訊息中使用的每個實體。
訊息狀態行
狀態行由協議版本、數字狀態碼及其關聯的文字短語組成。這些元素之間用空格 (SP) 字元分隔。
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
HTTP 版本
支援 HTTP 1.1 版本的伺服器將返回以下版本資訊。
HTTP-Version = HTTP/1.1
狀態碼
狀態碼元素是一個 3 位整數,其中狀態碼的第一位數字定義了響應的類別,後兩位數字沒有任何分類作用。第一位數字有 5 個值。
| 序號 | 程式碼和描述 |
|---|---|
| 1 | 1xx: 資訊性 表示請求已接收,並且正在處理中。 |
| 2 | 2xx: 成功 表示操作已成功接收、理解和接受。 |
| 3 | 3xx: 重定向 表示必須採取進一步操作才能完成請求。 |
| 4 | 4xx: 客戶端錯誤 表示請求包含語法錯誤或無法完成。 |
| 5 | 5xx: 伺服器錯誤 表示伺服器未能完成一個明顯有效的請求。 |
HTTP 狀態碼是可擴充套件的,HTTP 應用程式不需要理解所有已註冊狀態碼的含義。所有狀態碼的列表已在單獨的章節中列出,供您參考。
響應報頭欄位
我們將在學習 HTTP 報頭欄位的單獨章節中學習通用報頭和實體報頭。現在,讓我們檢查一下響應報頭欄位是什麼。
響應報頭欄位允許伺服器傳遞有關響應的附加資訊,這些資訊不能放在狀態行中。這些報頭欄位提供有關伺服器以及對 Request-URI 標識的資源的進一步訪問的資訊。
Accept-Ranges
Age
ETag
Location
Proxy-Authenticate
Retry-After
Server
Vary
WWW-Authenticate
如果您要編寫自己的自定義 Web 客戶端和伺服器,則可以引入自定義欄位。
響應訊息示例
現在讓我們將所有內容組合在一起,為從 tutorialspoint.com 上執行的 Web 伺服器獲取 **hello.htm** 頁面的請求形成 HTTP 響應。
HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT Content-Length: 88 Content-Type: text/html Connection: Closed
<html> <body> <h1>Hello, World!</h1> </body> </html>
以下示例顯示了一個 HTTP 響應訊息,該訊息顯示當 Web 伺服器找不到請求的頁面時的錯誤情況。
HTTP/1.1 404 Not Found Date: Sun, 18 Oct 2012 10:36:20 GMT Server: Apache/2.2.14 (Win32) Content-Length: 230 Connection: Closed Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html> <head> <title>404 Not Found</title> </head> <body> <h1>Not Found</h1> <p>The requested URL /t.html was not found on this server.</p> </body> </html>
以下是 HTTP 響應訊息的示例,該訊息顯示當 Web 伺服器在給定的 HTTP 請求中遇到錯誤的 HTTP 版本時的錯誤情況。
HTTP/1.1 400 Bad Request Date: Sun, 18 Oct 2012 10:36:20 GMT Server: Apache/2.2.14 (Win32) Content-Length: 230 Content-Type: text/html; charset=iso-8859-1 Connection: Closed
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html> <head> <title>400 Bad Request</title> </head> <body> <h1>Bad Request</h1> <p>Your browser sent a request that this server could not understand.</p> <p>The request line contained invalid characters following the protocol string.</p> </body> </html>