个性化阅读
专注于IT技术分析

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.srcmini02.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
<html>
<body>
<h1> First Program</h1>
</body>
</html>

HEAD方法

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

HEAD /first.htm HTTP/1.1
User-Agent: Mozilla/69.0 (compatible; MSIE5.01; Windows 10)
Host: www.srcmini02.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 xmlns=" https://www.srcmini02.com/">string</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
<html>
<body>
<h1> Request Processed Successfully</h1>
</body>
</html>

放置方法

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

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

在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
<html>
<body>
<h1> The file was created.</h1>
</body>
</html>

删除方法

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

DELETE /first.htm HTTP/1.1
User-Agent: Mozilla/69.0 (compatible; MSIE5.01; Windows 10)
Host: www.srcmini02.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
<html>
<body>
<h1>URL deleted</h1>
</body>
</html>

连接方法

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

CONNECT www.srcmini02.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方法和其他选项。下面的示例请求在srcmini02.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

赞(0)
未经允许不得转载:srcmini » HTTP方法解析

评论 抢沙发

评论前必须登录!