一般来说,Web服务器默认的只支持Post和Get这两种“只读”的请求方法。但是随着Ajax XMLHttpRequest 和 REST风格应用的深入,我们发现Http 1.1协议还支持如下请求方法(Request Method):

  • OPTIONS
  • HEAD
  • DELETE
  • PUT
  • TRACE
  • CONNECT
Get是最常用的,就是向Web Server发请求“获取”资源;那么Post就是向Web Server“邮寄”一些封装的数据包获取资源,这两者方法严格的说都是“索取”行为。
顾名思义,Delete方法就是通过http请求删除指定的URL上的资源啦,Delete请求一般会返回3种状态码:
  • 200 (OK) - 删除成功,同时返回已经删除的资源
  • 202 (Accepted) - 删除请求已经接受,但没有被立即执行(资源也许已经被转移到了待删除区域)
  • 204 (No Content) - 删除请求已经被执行,但是没有返回资源(也许是请求删除不存在的资源造成的)
Put方法就不多废话了,就是往Web Server上直接扔资源(上传资源)嘛,不过实际操作起来可能会让诸位够晕一壶,E文定义如下:

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI. If a new resource is created, the origin server MUST inform the user agent via the 201 (Created) response. If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request. If the resource could not be created or modified with the Request-URI, an appropriate error response SHOULD be given that reflects the nature of the problem. The recipient of the entity MUST NOT ignore any Content-* (e.g. Content-Range) headers that it does not understand or implement and MUST return a 501 (Not Implemented) response in such cases.

If the request passes through a cache and the Request-URI identifies one or more currently cached entities, those entries SHOULD be treated as stale. Responses to this method are not cacheable.

The fundamental difference between the POST and PUT requests is reflected in the different meaning of the Request-URI. The URI in a POST request identifies the resource that will handle the enclosed entity. That resource might be a data-accepting process, a gateway to some other protocol, or a separate entity that accepts annotations. In contrast, the URI in a PUT request identifies the entity enclosed with the request -- the user agent knows what URI is intended and the server MUST NOT attempt to apply the request to some other resource. If the server desires that the request be applied to a different URI,

it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether or not to redirect the request.

A single resource MAY be identified by many different URIs. For example, an article might have a URI for identifying "the current version" which is separate from the URI identifying each particular version. In this case, a PUT request on a general URI might result in several other URIs being defined by the origin server.

HTTP/1.1 does not define how a PUT method affects the state of an origin server.

PUT requests MUST obey the message transmission requirements set out in section 8.2.

Unless otherwise specified for a particular entity-header, the entity-headers in the PUT request SHOULD be applied to the resource created or modified by the PUT.

上面说的都是虚的,实战才是硬道理!

  • 首先,我们要让Web Server支持Delete 和 Put请求方法,以大家熟悉的Tomcat为例:

在Tomcat的web.xml 文件中配置 org.apache.catalina.servlets.DefaultServlet 的初始化参数

[xhtml] view plain copy
  1. <init-param>
  2. <param-name>readonly</param-name>
  3. <param-value>false</param-value>
  4. </init-param>

readonly参数默认是true,即不允许delete和put操作,所以默认的通过XMLHttpRequest对象的put或者delete方法访问就会报告 http 403 forbidden 错误。

  • 接下来,从客户端通过 Ajax XMLHTTPRequest 发起 DELETE/PUT 请求:

    [javascript] view plain copy
    1. function getXMLHTTPRequest(){
    2. if (XMLHttpRequest)    {
    3. return new XMLHttpRequest();
    4. } else {
    5. try{
    6. return new ActiveXObject('Msxml2.XMLHTTP');
    7. }catch(e){
    8. return new ActiveXObject('Microsoft.XMLHTTP');
    9. }
    10. }
    11. }
    12. var req = getXMLHTTPRequest();
    13. req.open('DELETE','http://localhost/test.jsp',false);
    14. req.send(null);
    15. document.write(req.responseText);
  • WebDAV也需要使用到这2种Http请求方法。

Http协议的Delete和Put方法是做什么的?怎么用?相关推荐

  1. python中的请求方法_http协议的9种请求方法

    http协议的9种请求方法 在http/1.1协议中,定义了8种访问指定资源的方法,他们分别为 OPTIONS GET HEAD POST PUT PATCH DELETE TRACE CONNECT ...

  2. ORA-12560: TNS: 协议适配器错误的解决方法

    ============================================================== ORA-12560: TNS: 协议适配器错误的解决方法 造成ORA-12 ...

  3. 协议适配器错误的解决方法

    ORA-12560: TNS: 协议适配器错误的解决方法 2008年10月08日 星期三 10:31 Case: Microsoft Windows [版本 5.2.3790] (C) 版权所有 19 ...

  4. oracle协议适配器错误00530,ORA-12560: TNS: 协议适配器错误的解决方法

    Case: Microsoft Windows [版本 5.2.3790] (C) 版权所有 1985-2003 Microsoft Corp. C:\Documents and Settings\u ...

  5. MFC操作Excel2003的CRange类的Copy函数和Delete函数实现方法

    一.CRange类的Copy函数实现方法 具体步骤: 1.实例化一个CRange类对象range1,关联你要拷贝的区域: 2.实例化一个CRange类对象range2,关联你要将要粘贴到的区域: 3. ...

  6. HTTP协议中GET和POST方法的区别

    本文详细对比了HTTP协议中GET和POST方法的区别,部分内容可能有误,读者请自行甄别. 转自博客https://sunshinevvv.coding.me/blog/2017/02/09/Http ...

  7. 小米 samba linux,解决Win10.4无法访问samba协议小米路由盘修复方法

    原标题:解决Win10.4无法访问samba协议小米路由盘修复方法 近日,应该有不是朋友使用Win10升级到最新系统Windows 10 Fall Creators Update(秋季创意者更新),这 ...

  8. 三方协议接收节点不存在_三方协议扣款常见问题及解决方法

    三方协议扣款常见问题及解决方法 要看填写的用人单位名称是否与单位的有效印鉴名称一致,如不一致,协议无效:填写自己的`专业名称时,要与学校教务处的专业名称一致,不能简写.接下来由小编为大家整理出三方协议 ...

  9. win7旗舰版 Internet协议版本4的设置方法 --转载

    win7旗舰版 Internet协议版本4的设置方法 win7旗舰版Internet协议版本4的设置方法: 本篇博文出自百度回答 链接:https://zhidao.baidu.com/questio ...

最新文章

  1. CVPR | 让合成图像更真实,上交大提出基于域验证的图像和谐化
  2. Pytorch笔记(python--类与对象(class and module))
  3. java编程思想第四版第三章要点习题
  4. LeetCode 之Two Sum
  5. 前端学习(2459):账户设置
  6. Python网络编程(1)-socket
  7. C# 析构函数(Destructor)和终结器(Finalizer)——托管资源的释放
  8. Linux Unbunt 安装显卡驱动 简单方法
  9. win2008删除计算机用户,Windws Srv 2008内置账户
  10. svn+http+ad域
  11. layui totalRow 多层嵌套json_自定义 Behavior,实现嵌套滑动、平滑切换周月视图的日历...
  12. 设计模式之单例模式:7种单例设计模式(Java实现)
  13. 微商软文发布方法有哪些?文案怎么编辑?
  14. python乘法口诀表打印
  15. 【时间之外】区块链和BT的技术是孪生的吗?
  16. Matlab 动态心形线GIF图
  17. 科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛)
  18. 雷诺手表怎么换电池?雷诺手表更换电池的方法
  19. cas1584803-67-7|花菁染料齐岳试剂
  20. 电脑qq怎么设置远程桌面连接到服务器,QQ远程协助在哪个位置 qq远程协助如何使用...

热门文章

  1. html放缩都让内容处于正中间
  2. wine运行bat文件
  3. python输入一个人的名字_怎样用c语言做到输入一个人的名字才会输出一个心?
  4. 消灭星星网页版java代码,javascript实现消灭星星小游戏简单版
  5. csv 20位数据 如何打开可以预览完整数字_干货Python Pandas 做数据分析之玩转 Excel 报表分析...
  6. [译]模型-视图-提供器 模式
  7. Codeforces940(A-F)
  8. 【案例】护士发错药怎么处理?
  9. linux各种复制命令
  10. Android优化系列之apk瘦身