目录

HTTP - Methods

下面定义了HTTP/1.1的常用方法集,可以根据需求扩展该集。 这些方法名称区分大小写,必须以大写形式使用。

SN 方法和描述
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.iowiki.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.iowiki.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.iowiki.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.iowiki.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.iowiki.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服务器的网络连接。 以下示例请求与主机iowiki.com上运行的Web服务器建立连接:

CONNECT www.iowiki.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,也可以指定星号(*)来引用整个服务器。 以下示例请求在iowiki.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.iowiki.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.iowiki.com
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
<上一篇.HTTP - Responses
HTTP - Status Codes.下一篇>
↑回到顶部↑
WIKI教程 @2018