HTTP请求和响应2:方法(Method)
方法表明了客户端希望服务器对资源执行的动作,常用的方法包括:GET、HEAD、POST、PUT、TRACE、OPTIONS和DELETE,每个服务器可以实现这些方法中的部分或者全部。下面将对这些方法做一个详细的介绍。
GET
GET /seasonal/index-fall.html HTTP/1.1 Host: www.joes-hardware.com Accept: *响应报文如下:
HTTP/1.1 200 OK Content-Type: text/html Context-Length: 617 //body
HEAD
1)在不获取资源的情况下了解资源的状况;
2)通过查看响应中的状态码,看看某个对象是否存在;
3)通过查看首部,测试资源是否被修改了。
HEAD返回的首部应该和GET返回的首部完全相同。
PUT
请求报文:
PUT /product-list.txt HTTP/1.1 Host: www.joes-hardware.com Content-type: text/plain Content-length: 34 //body响应报文:
HTTP/1.1 201 Created Location: http://www.joes-hardware.com/product-list.txt Content-type: text/plain Context-length: 47 http://www.joes-hardware.com/product-list.txt
POST
请求报文:
POST /inventory-check.cgi HTTP/1.1 Host: www.joes-hardware.com Content-type: text/plain Content-length: 18 item-bandsaw 2647服务端交由对应程序处理后,发送响应报文:
HTTP/1.1 200 OK Content-type: text/plain Context-length: 37 The bandsaw model 2647 is in stock!
TRACE
请求报文:
TRACE /product-list.txt HTTP/1.1 Accept:* Host: www.joes-hardware.com报文经过代理服务器后:
TRACE /product-list.txt HTTP/1.1 Host: www.joes-hardware.com Accept: * Via: 1.1 proxy3.company.com服务器弹回的TRACE响应:
HTTP/1.1 200 OK Content-type: text/plain Content-length: 96 TRACE /product-list.txt HTTP/1.1 Host: www.joes-hardware.com Accept: * Via: 1.1 proxy3.company.comTRACE响应经过代理服务器后:
HTTP/1.1 200 OK Content-type: text/plain Content-length: 96 Via: 1.1 proxy3.company.com TRACE /product-list.txt HTTP/1.1 Host: www.joes-hardware.com Accept: * Via: 1.1 proxy3.company.comTRACE方法主要用于诊断,它主要的缺陷是它假定中间应用程序对各种不同类型请求(GET、HEAD、POST等)的处理都是相同的,如果实际情况不同,那么诊断将不准确。
OPTIONS
请求报文:
OPTIONS * HTTP/1.1 Host: www.joes-hardware.com Accept: *响应报文:
HTTP/1.1 200 OK Allow: GET, POST, PUT, OPTIONS Context-length: 0
DELETE
请求报文:
DELETE /product-list.txt HTTP/1.1 Host: www.joes-hardware.com响应报文:
HTTP/1.1 200 OK Content-type: text/plain Content-length: 54 //响应消息
扩展方法
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。