在接收並解釋請求訊息後,伺服器以HTTP響應訊息響應 -
以下部分解釋HTTP訊息中使用的每個實體 -
狀態行包括協定版本,後跟數位狀態程式碼及其相關的文字短語。元素由空格SP字元分隔。
Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
下面我們來看看上面狀態行中提到的每個部分。
支援HTTP版本1.1的伺服器返回以下版本資訊 -
HTTP-Version = HTTP/1.1
狀態程式碼元素是一個3
位整數,其中狀態程式碼的第一個數位定義了響應類,後兩個數位沒有任何分類角色。一共有5個型別的狀態程式碼,它們分別如下所示 -
編號 | 狀態程式碼 | 描述說明 |
---|---|---|
1 | 1xx | 表示收到請求並繼續處理。 |
2 | 2xx | 表示操作已成功接收,理解和接受。 |
3 | 3xx | 表示必須採取進一步操作才能完成請求。 |
4 | 4xx | 表示請求包含錯誤的語法或無法實現。 |
5 | 5xx | 表示伺服器無法滿足明顯有效的請求。 |
HTTP狀態程式碼是可延伸的,並且不需要HTTP應用程式來理解所有已註冊狀態程式碼的含義。
響應檔頭欄位允許伺服器傳遞有關無法放入狀態行的響應的其他資訊。這些頭欄位提供有關伺服器的資訊以及有關Request-URI
標識的資源的進一步存取。
- Accept-Ranges
- Age
- ETag
- Location
- Proxy-Authenticate
- Retry-After
- Server
- Vary`
-
WWW-Authenticate`
如果您希望編寫自己的自定義Web用戶端和伺服器,可以引入自定義欄位。
下面我們把它們放在一起,形成一個HTTP響應,用於從在tw511.com
上執行的web伺服器獲取hello.html
頁面的請求 -
HTTP/1.1 200 OK
Date: Mon, 27 Jul 2019 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Last-Modified: Wed, 22 Jul 2019 19:15:56 GMT
Content-Length: 88
Content-Type: text/html
Connection: Closed
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
以下是當Web伺服器找不到請求的頁面時,HTTP響應訊息顯示錯誤情況的範例 -
HTTP/1.1 404 Not Found
Date: Sun, 18 Oct 2019 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 /tw511.com.html was not found on this server.</p>
</body>
</html>
以下是HTTP響應訊息的範例,顯示Web伺服器在給定HTTP請求中遇到錯誤的HTTP版本時的錯誤情況 -
HTTP/1.1 400 Bad Request
Date: Sun, 18 Oct 2019 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>