HTTP方法

下麵定義了HTTP/1.1的常用方法集,可以根據需求擴展該集。這些方法名稱區分大小寫,必須以大寫形式使用。

編號 方法 描述
1 GET 它用於使用給定的URI從給定伺服器檢索資訊。使用GET的請求應僅檢索數據,並且不應對數據產生其他影響。
2 HEAD 它與GET相同,但僅傳輸狀態行和標頭部分。
3 POST 它用於將數據發送到伺服器。例如,使用HTML表單的客戶資訊,檔上載等。
4 PUT 它用上傳的內容替換目標資源的所有當前表示。
5 DELETE 它刪除URI給出的目標資源的所有當前表示。
6 CONNECT 它建立到由給定URI標識的伺服器的通道。
7 OPTIONS 它描述了目標資源的通信選項。
8 TRACE 它沿著目標資源的路徑執行消息環回測試。

GET方法

它通過在請求的URL部分中指定參數來從Web伺服器檢索數據。這是用於文檔檢索的主要方法。以下示例使用GET方法獲取hello.html -

GET /hello.html HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.xuhuhu.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

針對上述GET請求發出以下伺服器回應 -

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
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed

<html>
   <body>
      <h1>Hello, World!</h1>
   </body>
</html>

HEAD方法

它在功能上類似於GET,除了伺服器回復回應行和標頭,但沒有實體主體。以下示例使用HEAD方法獲取有關hello.html的標頭資訊 -

HEAD /hello.html HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.xuhuhu.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

針對上述GET請求發出以下伺服器回應 -

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
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed

您應該注意到了,伺服器在標頭之後不發送任何數據。

POST方法

當您想要將某些數據發送到伺服器時使用POST方法。例如,檔更新,表單數據等。下麵的簡單示例使用POST方法將表單數據發送到由process.cgi處理的伺服器,最後返回回應 -

POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.xuhuhu.com
Content-Type: text/xml; charset = utf-8
Content-Length: 88
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

<?xml version = "1.0" encoding = "utf-8"?>
<string xmlns = "http://kaops.com/">string</string>

伺服器端腳本process.cgi處理傳遞的數據併發送以下回應 -

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 2009 19:15:56 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 88
Content-Type: text/html
Connection: Closed

<html>
   <body>
      <h1>Request Processed Successfully</h1>
   </body>
</html>

PUT方法

PUT方法用於請求伺服器將包含的實體主體存儲在給定URL指定的位置。以下示例請求伺服器將給定的實體保存在伺服器根目錄的hello.html中 -

PUT /hello.html HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.xuhuhu.com
Accept-Language: en-us
Connection: Keep-Alive
Content-type: text/html
Content-Length: 182

<html>
   <body>
      <h1>Hello, World!</h1>
   </body>
</html>

伺服器將給定的實體主體存儲在hello.html檔中,並將以下回應發送回客戶端 -

HTTP/1.1 201 Created
Date: Mon, 27 Jul 2019 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed

<html>
   <body>
      <h1>The file was created.</h1>
   </body>
</html>

DELETE方法

DELETE方法用於請求伺服器刪除給定URL指定的位置的檔。以下示例請求伺服器刪除伺服器根目錄下的給定檔hello.html -

DELETE /hello.html HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.xuhuhu.com
Accept-Language: en-us
Connection: Keep-Alive

伺服器刪除指定的檔hello.html並將以下回應發送回客戶端 -

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2019 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed

<html>
   <body>
      <h1>URL deleted.</h1>
   </body>
</html>

CONNECT方法

客戶端使用它通過HTTP建立到Web伺服器的網路連接。以下示例請求與主機xuhuhu.com上運行的Web伺服器建立連接 -

CONNECT www.xuhuhu.com HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

與伺服器建立連接,並將以下回應發送回客戶端 -

HTTP/1.1 200 Connection established
Date: Mon, 27 Jul 2019 12:28:53 GMT
Server: Apache/2.2.14 (Win32)

OPTIONS方法

客戶端使用它來查找Web伺服器支持的HTTP方法和其他選項。客戶端可以指定OPTIONS方法的URL,也可以指定星號(*)來引用整個伺服器。以下示例請求在xuhuhu.com上運行的Web伺服器支持的方法列表 -

OPTIONS * HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

伺服器根據伺服器的當前配置發送回應的資訊,例如 -

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2019 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Type: httpd/unix-directory

TRACE方法

它用於將HTTP請求的內容回送給請求者,請求者可以在開發時用於調試目的。以下示例顯示了TRACE方法的用法 -

TRACE / HTTP/1.1
Host: www.xuhuhu.com
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

伺服器將發送以下消息以回應上述請求 -

HTTP/1.1 200 OK
Date: Mon, 27 Jul 2019 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
Connection: close
Content-Type: message/http
Content-Length: 39

TRACE / HTTP/1.1
Host: www.xuhuhu.com
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)

上一篇: HTTP協議基礎 下一篇: HTTPS協議基礎