`
xutao5641745
  • 浏览: 227063 次
  • 性别: Icon_minigender_1
  • 来自: 汨罗
社区版块
存档分类
最新评论

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

阅读更多
 

原文出自:http://blog.csdn.net/kthq/article/details/5157822#

 

 

 

一般来说,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 的初始化参数

 

一般来说,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 的初始化参数

 

    <init-param>    
        <param-name>readonly</param-name>    
        <param-value>false</param-value>   
    </init-param>  
 

 

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

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

 

function getXMLHTTPRequest(){  
    if (XMLHttpRequest)    {  
        return new XMLHttpRequest();  
    } else {  
        try{  
            return new ActiveXObject('Msxml2.XMLHTTP');  
        }catch(e){  
            return new ActiveXObject('Microsoft.XMLHTTP');  
        }  
    }  
}  
var req = getXMLHTTPRequest();  
req.open('DELETE','http://localhost/test.jsp',false);  
req.send(null);  
document.write(req.responseText); 

 WebDAV也需要使用到这2种Http请求方法。

 

 

分享到:
评论

相关推荐

    Http协议的Delete和Put方法用法

    一般来说,Web服务器默认的只支持Post和Get...但是随着Ajax XMLHttpRequest 和 REST风格应用的深入,我们发现Http 1.1协议还支持如下请求方法(Request Method): •OPTIONS •HEAD •DELETE •PUT •TRACE •CONNECT

    包含post、delete、put、get方法四个restful支持的服务

    在这个服务中,提到了四个主要的HTTP方法:POST、DELETE、PUT和GET,这些方法分别对应于创建、删除、更新和查询资源的操作。 1. **POST** 方法:在RESTful API中,POST方法常用于创建新的资源。客户端发送一个POST...

    VC++ HTTP POST/GET/PUT/DELETE实现Rest API操作

    在本文中,我们将深入探讨如何使用VC++实现HTTP的基本操作,包括POST、GET、PUT和DELETE,以及如何处理代理设置、读取HTTP头信息和解析HTML文档中的特定标签属性值。这些功能对于开发RESTful API客户端或者进行网络...

    关于get.post delete put 的用法

    HTTP 方法详解 在 HTTP 中,定义了多种方法来与服务器进行交互,...HTTP 方法是 HTTP 协议的核心部分,每种方法都有其特定的用途和限制。了解每种方法的用途和限制对于开发者来说非常重要,以便更好地使用 HTTP 协议。

    http接口通用调试工具,支持get、post、put、patch、delete方法

    本文将深入探讨一个通用的HTTP接口调试工具,该工具支持GET、POST、PUT、PATCH和DELETE等常见HTTP请求方法,并且专注于处理JSON格式的数据。这将帮助开发者在开发和测试过程中更加高效地进行接口调用与验证。 首先...

    java http 发送 put delete post get请求

    本篇将详细解释如何使用Java发送PUT、DELETE、POST和GET这四种主要的HTTP请求。 PUT请求常用于更新已有资源,它的特点是替换目标URL指定的整个资源。在Java中,可以使用HttpURLConnection或Apache HttpClient库来...

    UniApp中封装request网络请求包含GET、POST、PUT、DELETE等

    本教程将详细介绍如何在UniApp中封装request网络请求,包括GET、POST、PUT和DELETE四种常见的HTTP方法。 首先,我们需要了解HTTP协议中的这四种方法: 1. GET:获取资源,是最常用的方法,用于从服务器上获取特定的...

    Web应用安全:HTTP协议GET和POST的使用区别文本.docx

    PUT方法用于更新已有资源,其行为类似于替换,即用请求主体中的数据完全替换掉服务器上指定URL的资源。DELETE方法则请求服务器删除指定的资源,但HTTP规范允许服务器在不告知客户端的情况下取消删除请求。 HEAD方法...

    关于HTTP协议禁用不常用方法漏洞的解决方案.docx

    例如,PUT和DELETE方法可以被用于修改或删除服务器上的资源,而TRACE方法则有可能泄露敏感信息。 #### 安全风险分析 1. **PUT和DELETE方法**:若服务器启用了这两个方法,恶意用户可能会利用它们来修改或删除服务器...

    Http协议中POST和GET方法的本质区别

    HTTP中的POST和GET方法是两种最常用于网页开发的数据传输方式,它们在原理和表现形式上存在本质区别。 首先,从原理上看,GET方法通常用于获取资源信息,它通过URL来传递参数,URL全称为资源描述符,表示网络上的一...

    HTTP协议详解(真的很经典)[归类].pdf

    HTTP 请求方法包括 GET、POST、HEAD、PUT、DELETE、TRACE、CONNECT、OPTIONS 等。每种方法规定了客户与服务器联系的类型不同。 GET 方法:请求获取 Request-URI 所标识的资源。 POST 方法:请求服务器接受附在请求...

    认清http协议本质

    2. HTTP方法:HTTP定义了多种请求方法,常见的有GET(获取资源)、POST(提交数据)、PUT(更新资源)、DELETE(删除资源)等。GET是最常用的方法,用于获取服务器上的资源;POST常用于提交表单数据,创建新的资源。...

    HTTP协议详解及RFC2616(HTTP)中文版

    HTTP/1.1是HTTP协议的一个重要版本,相比之前的HTTP/1.0,它引入了许多改进和新特性,如持久连接、分块传输编码和更多请求方法(如PUT、DELETE)。 **HTTP请求方法** HTTP协议定义了多种请求方法,常见的有GET、...

    http协议中文版(http协议中文版)

    2. **请求方法**:详述GET、POST、PUT、DELETE等方法的含义和使用场景,以及HEAD、OPTIONS、PATCH等其他方法。 3. **请求头**:请求头包含了许多关键信息,如User-Agent表示客户端信息,Accept表示客户端可接受的...

    HTTP PUT相关资料

    HTTP PUT方法是HTTP协议中的一种请求方法,用于向服务器上传或更新指定资源。在Web服务和API设计中,PUT通常用于替换已存在的资源,而不仅仅是添加新内容。这个压缩包文件包含了关于如何在Java中使用...

    HTTP协议详解PDF

    除此之外,还有PUT、DELETE、HEAD等其他方法,它们各自在不同的场景下有着特定的应用。 HTTP请求报文由三部分组成:请求行、请求头部和请求主体。请求行包含了方法、URL和HTTP版本信息,例如"GET /index....

    HTTP协议详解_HTTP协议_

    1. 请求方法:常见的HTTP请求方法有GET、POST、PUT、DELETE等,每个方法对应不同的资源操作。 2. URL(统一资源定位符):用于标识网络上的唯一资源,包括协议类型、主机名、端口号和路径。 3. 头部字段:HTTP请求和...

    张孝祥_HTTP协议详解第四讲

    总结来说,"张孝祥_HTTP协议详解第四讲"深入浅出地讲解了HTTP协议的关键要素,包括其工作原理、请求响应流程、请求方法、版本差异、状态码、安全性和缓存机制。通过这堂课的学习,我们可以更好地理解和应用HTTP协议...

    HTTP协议.zip_HTTP_HTTP协议

    2. **CRUD操作**: 使用HTTP方法对应资源的操作,GET获取资源,POST创建资源,PUT更新资源,DELETE删除资源。 3. **状态码反馈**: 通过状态码告知客户端操作结果,如201表示资源已创建,204表示无内容。 ### 总结 ...

Global site tag (gtag.js) - Google Analytics