- HTTP 教程
- HTTP - 首页
- HTTP - 概述
- HTTP - 参数
- HTTP - 消息
- HTTP - 请求
- HTTP - 响应
- HTTP - 方法
- HTTP - 状态码
- HTTP - 首部字段
- HTTP - 缓存
- HTTP - URL 编码
- HTTP - 安全性
- HTTP - 消息示例
- HTTP 有用资源
- HTTP - 快速指南
- HTTP - 有用资源
HTTP - 方法
HTTP/1.1 的常用方法集定义如下,并且可以根据需要扩展此集合。这些方法名称区分大小写,必须使用大写。
| 序号 | 方法及描述 |
|---|---|
| 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.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
<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.tutorialspoint.com Accept-Language: en-us Accept-Encoding: gzip, deflate Connection: Keep-Alive
服务器针对上述 HEAD 请求的响应如下
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 方法。以下示例使用 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
<?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.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文件中,并将以下响应发送回客户端
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 方法
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
<html> <body> <h1>URL deleted.</h1> </body> </html>
CONNECT 方法
客户端使用 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 方法
客户端使用 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 方法
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)