下面定義了HTTP/1.1的一組常用方法,可以根據需要擴展該組。這些方法名區分大小寫,必須使用大寫。
S.N. | Method and Description |
---|---|
1 | GET GET方法用於使用給定的URI從給定的伺服器檢索信息。使用GET的請求應該只檢索數據,不應該對數據有其他影響。 |
2 | HEAD 與GET相同,但僅傳輸狀態行和標題部分。 |
3 | POST POST請求用於使用HTML表單向伺服器發送數據,例如客戶信息、文件上載等。 |
4 | PUT 用上載的內容替換目標資源的所有當前表示形式。 |
5 | DELETE 刪除由URI給定的目標資源的所有當前表示形式。 |
6 | CONNECT 建立到由給定URI標識的伺服器的隧道。 |
7 | OPTIONS 描述目標資源的通信選項。 |
8 | TRACE 沿目標資源的路徑執行消息循環回測試。 |
GET Method
GET請求通過在請求的URL部分指定參數從web伺服器檢索數據。這是文獻檢索的主要方法。以下示例使用GET方法獲取hello.htm:
GET /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.com Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: Keep-Alive
針對上述GET請求的伺服器響應如下:
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 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 Method
HEAD方法在功能上與GET類似,只是伺服器使用響應行和頭進行響應,但沒有實體體。以下示例使用HEAD方法獲取有關hello.htm的頭信息:
HEAD /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.com Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: Keep-Alive
針對上述GET請求的伺服器響應如下:
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 ETag: "34aa387-d-1568eb00" Vary: Authorization,Accept Accept-Ranges: bytes Content-Length: 88 Content-Type: text/html Connection: Closed
您可以注意到,這裡的伺服器在頭之後不發送任何數據。
POST Method
POST方法用於將某些數據發送到伺服器,例如文件更新、表單數據等。以下示例使用POST方法將表單數據發送到伺服器,伺服器將由process.cgi處理並最終返迴響應:
POST /cgi-bin/process.cgi HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.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://clearforest.com/">string</string>
伺服器端腳本process.cgi處理傳遞的數據並發送以下響應:
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 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 Method
PUT方法用於請求伺服器將包含的實體體存儲在給定URL指定的位置。以下示例請求伺服器將給定的實體體保存在伺服器根目錄下的hello.htm中:
PUT /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.com Accept-Language: en-us Connection: Keep-Alive Content-type: text/html Content-Length: 182
<html> <body> <h1>Hello, World!</h1> </body> </html>
伺服器將把給定的實體體存儲在hello.htm文件中,並將以下響應發送回客戶端:
HTTP/1.1 201 Created Date: Mon, 27 Jul 2009 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 Method
DELETE方法用於請求伺服器在給定URL指定的位置刪除文件。以下示例請求伺服器刪除伺服器根目錄下的給定文件hello.htm:
DELETE /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.tutorialspoint.com Accept-Language: en-us Connection: Keep-Alive
伺服器將刪除上述文件hello.htm,並將以下響應發送回客戶端:
HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 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 Method
CONNECT www.tutorialspoint.com HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
與伺服器建立連接,並將以下響應發送回客戶端:
HTTP/1.1 200 Connection established Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32)
OPTIONS Method
OPTIONS * HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
伺服器將根據伺服器的當前配置發送信息,例如:
HTTP/1.1 200 OK Date: Mon, 27 Jul 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Allow: GET,HEAD,POST,OPTIONS,TRACE Content-Type: httpd/unix-directory
TRACE Method
TRACE方法用於將HTTP請求的內容回顯給請求者,該請求者可以在開發時用於調試目的。以下示例顯示跟蹤方法的用法:
TRACE / HTTP/1.1 Host: www.tutorialspoint.com User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
伺服器將響應上述請求發送以下消息: