Http 简明教程
HTTP - Methods
以下定义了 HTTP/1.1 的常用方法集,并且此集合可根据需要扩大。这些方法名称区分大小写,并且必须使用大写。
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*A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms. |
4 |
*PUT*Replaces all current representations of the target resource with the uploaded content. |
5 |
*DELETE*Removes all current representations of the target resource given by a URI. |
6 |
*CONNECT*Establishes a tunnel to the server identified by a given URI. |
7 |
*OPTIONS*Describes the communication options for the target resource. |
8 |
*TRACE*Performs a message loop-back test along the path to the target resource. |
GET Method
GET 请求通过在请求的 URL 部分中指定参数,从 Web 服务器获取数据。这是用于文档检索的主要方法。以下示例使用了 GET 方法来获取 hello.htm:
A GET request retrieves data from a web server by specifying parameters in the URL portion of the request. This is the main method used for document retrieval. The following example makes use of GET method to fetch 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 请求的响应如下:
The server response against the above GET request will be as follows:
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 的标头信息:
The HEAD method is functionally similar to GET, except that the server replies with a response line and headers, but no entity-body. The following example makes use of HEAD method to fetch header information about 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 请求的响应如下:
The server response against the above HEAD request will be as follows:
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
可以注意到这里服务器在标头后不发送任何数据。
You can notice that here server the does not send any data after header.
POST Method
POST 方法用于希望向服务器发送某些数据时,例如文件更新、表单数据等。以下示例使用了 POST 方法向服务器发送表单数据,该表单数据将由 process.cgi 处理,最终返回响应:
The POST method is used when you want to send some data to the server, for example, file update, form data, etc. The following example makes use of POST method to send a form data to the server, which will be processed by a process.cgi and finally a response will be returned:
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 处理传递的数据并发送以下响应:
The server side script process.cgi processes the passed data and sends the following response:
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 中:
The PUT method is used to request the server to store the included entity-body at a location specified by the given URL. The following example requests the server to save the given entity-body in hello.htm at the root of the server:
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 文件中,并将以下响应发送回客户端:
The server will store the given entity-body in hello.htm file and will send the following response back to the client:
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 :
The DELETE method is used to request the server to delete a file at a location specified by the given URL. The following example requests the server to delete the given file hello.htm at the root of the server:
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 ,并将以下响应发送回客户端:
The server will delete the mentioned file hello.htm and will send the following response back to the client:
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 方法由客户端用于通过 HTTP 与 Web 服务器建立网络连接。以下示例请求与在主机 tutorialspoint.com 上运行的 Web 服务器建立连接:
The CONNECT method is used by the client to establish a network connection to a web server over HTTP. The following example requests a connection with a web server running on the host tutorialspoint.com:
CONNECT www.tutorialspoint.com HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
与服务器建立连接后,以下响应被发送回客户端:
The connection is established with the server and the following response is sent back to the client:
HTTP/1.1 200 Connection established
Date: Mon, 27 Jul 2009 12:28:53 GMT
Server: Apache/2.2.14 (Win32)
OPTIONS Method
OPTIONS 方法由客户端用于查找 Web 服务器支持的 HTTP 方法和其他选项。客户端可以为 OPTIONS 方法指定一个 URL,或一个星号 (*) 来指整个服务器。以下示例请求正在 tutorialspoint.com 上运行的 Web 服务器支持的方法列表:
The OPTIONS method is used by the client to find out the HTTP methods and other options supported by a web server. The client can specify a URL for the OPTIONS method, or an asterisk (*) to refer to the entire server. The following example requests a list of methods supported by a web server running on tutorialspoint.com:
OPTIONS * HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
服务器将发送基于服务器当前配置的信息,例如:
The server will send an information based on the current configuration of the server, for example:
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 方法的用法:
The TRACE method is used to echo the contents of an HTTP Request back to the requester which can be used for debugging purpose at the time of development. The following example shows the usage of TRACE method:
TRACE / HTTP/1.1
Host: www.tutorialspoint.com
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
服务器将发送以下消息来响应以上请求:
The server will send the following message in response to the above request:
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)