HTTP 方法 GET、POST、PUT 和 DELETE 之间有什么区别 [英] What is difference between HTTP methods GET, POST, PUT and DELETE

查看:53
本文介绍了HTTP 方法 GET、POST、PUT 和 DELETE 之间有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 REST WCF 服务,理论上我知道什么时候选择什么目的.

I am developing REST WCF service and As theoretically i know when to opt for what purpose.

  • GET 获取资源
  • PUT 更新
  • POST 插入
  • DELETE 删除
  • GET to get the resource
  • PUT to update
  • POST to Insert
  • DELETE to delete

但是如果我们不遵循上述规则,假设插入一条记录,我使用了GET方法有什么缺点?

But what is the disadvantage if we don't follow this above rule, suppose to insert a record i used GET method?

推荐答案

因为 HTTP GET 方法被指定为幂等的,所以根据规范,可以重新提交 GET 请求,并假设它不会更改服务器上的任何内容.这不是 HTTP POST 的情况,根据规范可以更改服务器上运行的应用程序的状态.

Because the HTTP GET method is specified as idempotent, a GET request, by specification, can be resubmitted with the assumption that it will not change anything on the server. This is not the case for a HTTP POST which by specification can change the status of the application running on the server.

因此,根据规范,可以对页面执行 N 次 HTTP GET,而不必担心更改其状态.

So, by specification, one can perform an HTTP GET against a page N number of times without worrying of being changing its status.

不遵守规范可能会产生各种不良后果.例如,网络爬虫通过 GET 请求来索引站点,而不是 POST.如果您允许 HTTP GET 请求对数据库进行更改,您就可以轻松理解它可能产生的不良影响.

Not respecting the specification may have various undesired results. For example, Web crawlers follow through GET request to index a site, but not POST. If you allowed an HTTP GET request to make changes to the database, you can easily understand the undesired implication it can have.

尊重规范就像尊重您的服务或网站与一系列不同消费者之间的协议,这些消费者可以是普通用户的浏览器,也可以是网络爬虫等其他服务.

Respecting a specification is like respecting an agreement between your service or website and an array of different consumers which can be normal users' browsers but also other services like web crawlers.

您可以构建一个使用 GET 插入记录的站点,但您还应该期望围绕您的站点构建的任何内容都可以在假设您遵守协议的情况下正常运行.

You could build a site that uses a GET to insert a record, but you should also expect that whatever is built around to consume your site is functioning with the assumption that you are respecting the agreement.

作为最后一个示例,当用户尝试刷新由 HTTP POST 请求访问的页面时,Web 浏览器会警告用户可能会重新提交某些数据.如果通过 HTTP GET 请求访问该页面,则您不会获得内置浏览器的那层保护.

As a last example, web browsers warn users when they try to refresh a page that was reached by a HTTP POST request warning that some data might be resubmitted. You do not get that layer of protection built-in browsers if the page is reached by a HTTP GET request.

您可以在此处阅读更多信息:http://www.w3.org/协议/rfc2616/rfc2616-sec9.html

You can read more here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

这篇关于HTTP 方法 GET、POST、PUT 和 DELETE 之间有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆