.net HTTP 方法 GET、POST、PUT 和 DELETE 之间有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18395523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:25:28  来源:igfitidea点击:

What is difference between HTTP methods GET, POST, PUT and DELETE

.netwcfweb-servicesresthttp-method

提问by Fooker

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

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

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

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

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

回答by Giuseppe Romagnuolo

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.

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

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

因此,根据规范,可以对页面执行 N 次 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.

不遵守规范可能会产生各种不希望的结果。例如,网络爬虫通过 GET 请求来索引站点,而不是 POST。如果您允许 HTTP GET 请求对数据库进行更改,您就可以很容易地理解它可能具有的不良影响。

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.

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

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.

您可以构建一个使用 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 POST 请求访问的页面时,Web 浏览器会警告用户可能会重新提交某些数据。如果通过 HTTP GET 请求访问页面,则您不会获得内置浏览器的那层保护。

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

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

回答by Dan

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

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

Search engines access your pages using GET requests, so if you did this, google's crawler might insert records that you didn't want.

搜索引擎使用 GET 请求访问您的页面,因此如果您这样做,谷歌的爬虫可能会插入您不想要的记录。

Often, people will use POST for any kind of ajax request, with the actual action in the request's body. There's nothing very wrong with this, but the feature is there for you to use, so you might as well use it.

通常,人们会将 POST 用于任何类型的 ajax 请求,并在请求正文中使用实际操作。这没有什么大错,但该功能可供您使用,因此您不妨使用它。

回答by pushya

I faced a situation i should have used the PUT instead of GET. I had a permission insertion call going to a third party( this was google). I spin a Ajax GET request for update permission call to my Servlet and from their the call went to external service. The external service took considerable amount of time to finish the request. In the mean time I was seeing duplication of same permission call in my server logs. It was browser which keep on calling the server saying are you done? since it is a GET and browser can call the server as many times as possible. Browser followed the standard and my code did not. I had the issue for not following standard.

我遇到了一种情况,我应该使用 PUT 而不是 GET。我有一个许可插入电话给第三方(这是谷歌)。我将 Ajax GET 请求用于更新权限调用到我的 Servlet,并且从他们的调用转到外部服务。外部服务花费了大量时间来完成请求。与此同时,我在服务器日志中看到重复的相同权限调用。是浏览器不断调用服务器说你完成了吗?因为它是一个 GET,浏览器可以尽可能多地调用服务器。浏览器遵循标准而我的代码没有。我遇到了不遵守标准的问题。