深入理解 HTTP 协议 POST 与PUT 方法区别

来源:互联网 发布:java教程入门视频 编辑:程序博客网 时间:2024/06/06 05:33

概况

PUT与POST 基本区别反映在Request-URI  的不同意义,PUT  是属于幂等性的方法,提交相同的内容不管提交几次返回的内容都相同,而POST 方法不具有这一属性。

例如:某张表的数据库主键设置了自增属性, 当提交数据的时候服务器都会创建一条新的记录,那么是不能使用PUT 方法来对此资源进行操作的。


HTTP/1.1 规范  (RFC2616) 第九章 9.1 提到了两个概念:Safe Methods 和 Idempotent Methods , 其中

Idempotent Methods( 幂等方法)  定义

<span style="font-size:14px;">Methods can also have the property of "idempotence" in that (aside from error or expiration issues) the side-effects of N > 0identical requests is the same as for a single request.The methods GET, HEAD,   PUT and DELETE share this property. Also, the methods OPTIONS and   TRACE SHOULD NOT haveside effects, and so are inherently idempotent.HTTP方法的幂等性是指一次和多次请求某一个资源应该具有同样的副作用. 其中包括GET, HEAD,PUT and DELETE</span><span style="font-size:14px;"> 方法。而后在9.6 中对POST与PUT这两个方法进行了比较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 adata-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,</span><span style="font-size:14px;"></span><p>it MUST send a 301 (Moved Permanently) response; the user agent MAY then make its own decision regarding whether </p><p>or not to redirect the request.A single resource MAY be identified by many different URIs. </p><p>For example, an article might have a URI for identifying "the current version" which is separate from the URI </p><p>identifying each particular   version. In this case, a PUT request on a general URI might result in several </p><p>other URIs being defined by the origin server.</p><span style="font-size:14px;">POST 和 PUT请求间的基本区别反映在 Request-URI 的不同意义。POST 请求中 URI 标 识的资源将处理封装的实体。该资源可能是数据接收过程、其它协议的网关或接受注解的独立实体。与此对应,PUT 请求中的 URI 标识请求封装的实体——用户代理知道该 URI 是目 标且服务器不能试图将该请求应用到其它资源上。如果服务器希望该请求应用到不同的URI 上,则它必须发送301(Moved Permanently)请求;这时客户代理可以自己决定是否要重定向该请求。可以用许多不同的 URI 标识同个资源。例如,一篇文章可以有标识为“当前版本”的 URI,它独立于标识每个特别版本的 URI。在这种情况下,使用通用 URI 的 PUT 请求可能 造成原始服务器定义的一些不同 URI 的结果。</span>






0 0