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协议基础