The set of common methods for HTTP/1.1 is defined below and this set can be expanded based on requirements. These method names are case sensitive and they must be used in uppercase.
S.N. | Method and Description |
---|---|
1 | GET The GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data. |
2 | HEAD Same as GET, but transfers the status line and header section only. |
3 | POST POST请求用于使用HTML表单向服务器发送数据,例如客户信息,文件上传等。 |
4 | 放 使用上传的内容替换目标资源的所有当前表示。 |
5 | 删除 删除由URI给出的目标资源的所有当前表示。 |
6 | CONNECT 建立通过给定URI标识的服务器的隧道。 |
7 | 选项 描述目标资源的通信选项。 |
8 | 跟踪 沿着目标资源的路径执行消息循环测试。 |
GET请求通过指定请求的URL部分中的参数从Web服务器检索数据。这是文档检索的主要方法。以下示例使用GET方法来获取hello.htm:
GET /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT) Host: www.vue5.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方法在功能上类似于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方法,例如文件更新,表单数据等。以下示例使用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方法用于请求服务器将包含的实体体存储在给定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方法用于请求服务器在给定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方法通过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方法来查找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方法用于将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)