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.jc2182.com
    Accept-Language: en-us
    Accept-Encoding: gzip, deflate
    Connection: Keep-Moove
    
    针对上述 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
    
    
    <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.jc2182.com
    Accept-Language: en-us
    Accept-Encoding: gzip, deflate
    Connection: Keep-Moove
    
    针对上述 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.jc2182.com
    Content-Type: text/xml; charset=utf-8
    Content-Length: 88
    Accept-Language: en-us
    Accept-Encoding: gzip, deflate
    Connection: Keep-Moove
    
    
    <?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.jc2182.com
    Accept-Language: en-us
    Connection: Keep-Moove
    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.jc2182.com
    Accept-Language: en-us
    Connection: Keep-Moove
    
    服务器将删除提到的文件 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 服务器的网络连接。以下示例请求与主机 jc2182.com 上运行的 Web 服务器建立连接:
    
    CONNECT www.jc2182.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,或者一个星号 (*) 来引用整个服务器。以下示例请求运行在 jc2182.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.jc2182.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.jc2182.com
    User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)