📅  最后修改于: 2020-12-14 05:21:36             🧑  作者: Mango
HTTP / 1.1的一组通用方法在下面定义,并且可以根据要求扩展此组。这些方法名称区分大小写,必须使用大写字母。
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请求通过在请求的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
Hello, World!
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
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
Request Processed Successfully
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
Hello, World!
服务器将给定的实体主体存储在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
The file was created.
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
URL deleted.
客户端使用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)