📜  HTTP方法

📅  最后修改于: 2021-01-03 03:08:44             🧑  作者: Mango

HTTP方法

对于HTTP / 1.1,下面定义了一组常用方法。可以根据要求扩展此集合。这些方法的名称区分大小写,必须使用大写字母。

方法和说明

i)获取

此方法使用给定的URI从给定的服务器检索信息。 GET请求可以检索数据。它不能对数据施加其他影响。

ii)头

此方法与GET方法相同。它仅用于传输状态行和标题部分。

iii)开机自检

POST请求将数据发送到服务器。例如,使用HTML表单的文件上传,客户信息等。

iv)放置

PUT方法用于用上载的内容替换目标资源的所有当前表示形式。

v)删除

DELETE方法用于删除目标资源的所有当前表示形式,该表示形式由URI给出。

vi)连接

此方法建立到服务器的隧道,该隧道由给定的URI标识。

vii)选项

此方法描述了目标资源的通信选项。

GET方法

此方法用于使用请求的URL部分中的指定参数从Web服务器检索数据。这是用于文档检索的主要方法。使用GET方法来获取first.htm的方法如下:

GET /first.htm HTTP/1.1
User-Agent: Mozilla/69.0 (compatible; MSIE5.01; Windows 10)
Host: www.javatpoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

以下是服务器对以上GET请求的响应:

HTTP/1.1 200 OK
Date: Wed, 4 Dec 2019 5:15:40 GMT
Server: Apache/2.4. 41 (Win32)
Last-Modified: Mon, 2 Dec 2019 15:40:30 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 55
Content-Type: text/html
Connection: Closed


First Program

HEAD方法

此方法与GET方法相同。但是在HEAD方法中,服务器使用响应行和标头进行回复,而没有实体主体。使用HEAD方法来获取有关first.htm的标头信息的方法如下:

HEAD /first.htm HTTP/1.1
User-Agent: Mozilla/69.0 (compatible; MSIE5.01; Windows 10)
Host: www.javatpoint.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive

以下是针对上述HEAD请求的服务器响应:

HTTP/1.1 200 OK
Date: Wed, 4 Dec 2019 5:15:40 GMT
Server: Apache/2.4. 41 (Win32)
Last-Modified: Mon, 2 Dec 2019 15:40:30 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 55
Content-Type: text/html
Connection: Closed

在这里,我们可以看到服务器在标头之后没有发送任何数据。

开机自检方法

此方法用于将一些数据发送到服务器,例如,从数据更新文件等。使用POST方法将表单数据发送到服务器的方法如下:

POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/69.0 (compatible; MSIE5.01; Windows 10)
Host: www.jsvatpoint.com
Content-Type: text/xml; charset=utf-5
Content-Length: 55
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
<"xml version="1.0" encoding="utf-5">
string

服务器端process.cgi的脚本处理传递的数据并发送如下响应:

HTTP/1.1 200 OK
Date: Wed, 4 Dec 2019 5:15:40 GMT
Server: Apache/2.4. 41 (Win32)
Last-Modified: Mon, 2 Dec 2019 15:40:30 GMT
ETag: "34aa387-d-1568eb00"
Vary: Authorization,Accept
Accept-Ranges: bytes
Content-Length: 55
Content-Type: text/html
Connection: Closed


Request Processed Successfully

放置方法

此方法请求服务器在给定URL指定的位置存储所包含的实体。以下示例请求服务器将给定的实体正文保存在服务器根目录中的first.htm中。

PUT /first.htm HTTP/1.1
User-Agent: Mozilla/69.0 (compatible; MSIE5.01; Windows 10)
Host: www.javatpoint.com
Accept-Language: en-us
Connection: Keep-Alive
Content-type: text/html
Content-Length: 150


First Program

服务器将在first.htm文件中存储给定的实体主体,并将以下响应发送回客户端:

HTTP/1.1 201 Created
Date: Wed, 4 Dec 2019 5:15:40 GMT
Server: Apache/2.4. 41 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed


The file was created.

删除方法

此方法请求服务器在给定URL指定的位置删除文件。下面的示例请求服务器删除服务器根目录下的first.htm文件:

DELETE /first.htm HTTP/1.1
User-Agent: Mozilla/69.0 (compatible; MSIE5.01; Windows 10)
Host: www.javatpoint.com
Accept-Language: en-us
Connection: Keep-Alive

在上面的示例之后,服务器将删除first.htm文件,并将响应发送回客户端,如下所示:

HTTP/1.1 200 OK
Date: Wed, 4 Dec 2019 5:15:40 GMT
Server: Apache/2.4. 41 (Win32)
Content-type: text/html
Content-length: 30
Connection: Closed


URL deleted

连接方法

客户端使用此方法。它通过HTTP建立到Web服务器的网络连接。下面的示例请求与主机javatpoint.com上运行的Web服务器的连接:

CONNECT www.javatpoint.com HTTP/1.1
User-Agent: Mozilla/69.0 (compatible; MSIE5.01; Windows 10)

以下示例显示与服务器建立连接,并将响应发送回客户端:

HTTP/1.1 200 Connection established
Date: Wed, 4 Dec 2019 5:15:40 GMT
Server: Apache/2.4. 41 (Win32)

选项方法

客户端使用此方法。它用于查找Web服务器支持的HTTP方法和其他选项。下面的示例请求在javatpoint.com上运行的Web服务器支持的方法列表:

OPTIONS * HTTP/1.1
User-Agent: Mozilla/69.0 (compatible; MSIE5.01; Windows 10)

在下面的示例中,服务器将基于服务器的当前配置发送信息:

HTTP/1.1 200 OK
Date: Wed, 4 Dec 2019 5:15:40 GMT
Server: Apache/2.4. 41 (Win32)
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Type: httpd/unix-directory