
- HTTP 教程
- HTTP - 首頁
- HTTP - 概述
- HTTP - 引數
- HTTP - 訊息
- HTTP - 請求
- HTTP - 響應
- HTTP - 方法
- HTTP - 狀態碼
- HTTP - 首部欄位
- HTTP - 快取
- HTTP - URL 編碼
- HTTP - 安全性
- HTTP - 訊息示例
- HTTP 有用資源
- HTTP - 快速指南
- HTTP - 有用資源
HTTP - 方法
HTTP/1.1 的常用方法集定義如下,並且可以根據需要擴充套件此集合。這些方法名稱區分大小寫,必須使用大寫。
序號 | 方法及描述 |
---|---|
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 方法
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 方法
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
伺服器針對上述 HEAD 請求的響應如下
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 方法
當您想要向伺服器傳送一些資料時,例如檔案更新、表單資料等,可以使用 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 方法
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 方法
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 方法
客戶端使用 CONNECT 方法透過 HTTP 建立到 Web 伺服器的網路連線。以下示例請求與在主機 tutorialspoint.com 上執行的 Web 伺服器建立連線
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 方法
客戶端使用 OPTIONS 方法找出 Web 伺服器支援的 HTTP 方法和其他選項。客戶端可以為 OPTIONS 方法指定 URL,也可以使用星號 (*) 來指代整個伺服器。以下示例請求在 tutorialspoint.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 2009 12:28:53 GMT Server: Apache/2.2.14 (Win32) Allow: GET,HEAD,POST,OPTIONS,TRACE Content-Type: httpd/unix-directory
TRACE 方法
TRACE 方法用於將 HTTP 請求的內容回顯給請求者,這可以在開發時用於除錯目的。以下示例顯示了 TRACE 方法的使用方法
TRACE / HTTP/1.1 Host: www.tutorialspoint.com 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) Connection: close Content-Type: message/http Content-Length: 39 TRACE / HTTP/1.1 Host: www.tutorialspoint.com User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)