教程 > http 教程 > 阅读:65

http 方法——迹忆客-ag捕鱼王app官网

下面定义了 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.jiyik.com
accept-language: zh-cn,zh;q=0.9
accept-encoding: gzip, deflate
connection: keep-alive

针对上述 head 请求的服务器响应如下:

http/1.1 200 ok
date: mon, 27 jul 2018 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



head 方法

head 方法在功能上与 get 类似,不同之处在于服务器使用响应行和标题进行回复,但没有实体正文。以下示例使用 head 方法获取有关 hello.htm 的标头信息:

head /hello.htm http/1.1
user-agent: mozilla/4.0 (compatible; msie5.01; windows nt)
host: www.jiyik.com
accept-language: zh-cn,zh;q=0.9
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 2018 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.jiyik.com
content-type: text/xml; charset=utf-8
content-length: 88
accept-language: zh-cn,zh;q=0.9
accept-encoding: gzip, deflate
connection: keep-alive

string

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

http/1.1 200 ok
date: mon, 27 jul 2018 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



put 方法

put 方法用于请求服务器将包含的实体主体存储在给定 url 指定的位置。以下示例请求服务器将给定的实体主体保存在服务器根目录下的hello.htm中:

put /hello.htm http/1.1
user-agent: mozilla/4.0 (compatible; msie5.01; windows nt)
host: www.jiyik.com
accept-language: zh-cn,zh;q=0.9
connection: keep-alive
content-type: text/html
content-length: 182


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

http/1.1 201 created
date: mon, 27 jul 2018 12:28:53 gmt
server: apache/2.2.14 (win32)
content-type: text/html
content-length: 30
connection: closed



delete 方法

delete 方法用于请求服务器删除给定 url 指定位置的文件。以下示例请求服务器删除服务器根目录下的给定文件hello.htm:

delete /hello.htm http/1.1
user-agent: mozilla/4.0 (compatible; msie5.01; windows nt)
host: www.jiyik.com
accept-language: zh-cn,zh;q=0.9
connection: keep-alive

服务器将删除提到的文件hello.htm并将以下响应发送回客户端:

http/1.1 200 ok
date: mon, 27 jul 2018 12:28:53 gmt
server: apache/2.2.14 (win32)
content-type: text/html
content-length: 30
connection: closed



connect 方法

客户端使用 connect 方法通过 http 建立到 web 服务器的网络连接。以下示例请求与在主机 jiyik.com 上运行的 web 服务器建立连接:

connect www.jiyik.com http/1.1
user-agent: mozilla/4.0 (compatible; msie5.01; windows nt)

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

http/1.1 200 connection established
date: mon, 27 jul 2018 12:28:53 gmt
server: apache/2.2.14 (win32)

options 方法

客户端使用 options 方法来找出 web 服务器支持的 http 方法和其他选项。客户端可以为 options 方法指定一个 url,或者一个星号 (*) 来引用整个服务器。以下示例请求在 jiyik.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 2018 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.jiyik.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.jiyik.com
user-agent: mozilla/4.0 (compatible; msie5.01; windows nt)

查看笔记

扫码一下
查看教程更方便
网站地图