据我所知,有三种类型:

不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。

我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?


当前回答

从w3schools.com:

What is HTTP? The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers. HTTP works as a request-response protocol between a client and server. A web browser may be the client, and an application on a computer that hosts a web site may be the server. Example: A client (browser) submits an HTTP request to the server; then the server returns a response to the client. The response contains status information about the request and may also contain the requested content. Two HTTP Request Methods: GET and POST Two commonly used methods for a request-response between a client and server are: GET and POST. GET – Requests data from a specified resource POST – Submits data to be processed to a specified resource

在这里我们区分主要的区别:

其他回答

来自RFC 2616:

9.3获得 GET方法意味着检索任何信息(形式为 实体)由 要求通用。如果Request-URI引用 对于数据生成过程,它是 应返回的已生成数据 作为响应中的实体而不是 进程的源文本,除非 这个文本恰好是的输出 这个过程。

9.5 POST The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. POST is designed to allow a uniform method to cover the following functions: Annotation of existing resources; Posting a message to a bulletin board, newsgroup, mailing list, or similar group of articles; Providing a block of data, such as the result of submitting a form, to a data-handling process; Extending a database through an append operation. The actual function performed by the POST method is determined by the server and is usually dependent on the Request-URI. The posted entity is subordinate to that URI in the same way that a file is subordinate to a directory containing it, a news article is subordinate to a newsgroup to which it is posted, or a record is subordinate to a database. The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.

如果您不介意请求被重复(即它不会改变状态),请使用GET。

如果操作确实改变了系统的状态,则使用POST。

没有什么是你本身做不到的。关键在于您不应该修改HTTP GET上的服务器状态。HTTP代理假设由于HTTP GET不修改状态,那么用户调用HTTP GET一次还是1000次没有区别。使用这些信息,他们认为返回第一个HTTP GET的缓存版本是安全的。如果你破坏了HTTP规范,你就有破坏HTTP客户端和代理的风险。不要这样做:)

在短暂的

使用GET来获得安全的和无效的请求 对于既不安全也不幂等的请求使用POST


在细节 每个人都有一个合适的位置。即使您不遵循RESTful原则,通过学习REST以及面向资源的方法是如何工作的,也可以获得很多东西。

RESTful应用程序将对安全且幂等的操作使用get。

安全操作是不改变请求数据的操作。

幂等运算是一种无论你请求多少次结果都相同的运算。

有理由认为,由于get用于安全操作,它们自动也是幂等的。通常,GET用于检索资源(例如,堆栈溢出时的问题及其相关答案)或资源集合。

RESTful应用程序将使用put进行不安全但幂等的操作。

我知道这个问题是关于GET和POST的,但是我马上回到POST。

通常,PUT用于编辑资源(例如,在堆栈溢出上编辑问题或答案)。

POST可以用于任何既不安全也不幂等的操作。

通常,POST将用于创建一个新资源,例如创建一个new SO问题(尽管在某些设计中,PUT也会用于此)。

如果你运行POST两次,你最终会创建两个新的问题。

还有一个DELETE操作,但我猜我可以把它留在那里:)

讨论

实际上,现代网络浏览器通常只可靠地支持GET和POST(你可以通过javascript调用来执行所有这些操作,但是在表单中输入数据和按提交,你通常有两个选项)。在RESTful应用程序中,POST通常也会被覆盖以提供PUT和DELETE调用。

但是,即使您没有遵循RESTful原则,也可以考虑使用GET来检索/查看信息,使用POST来创建/编辑信息。

永远不要对改变数据的操作使用GET。如果搜索引擎抓取到你邪恶行动的链接,或者客户书签,这可能会带来大麻烦。

一个实际的区别是浏览器和网络服务器对URL中可以存在的字符数量有限制。每个应用程序都不一样,但如果你的表单中有文本区域,它肯定是可以达到的。

get的另一个问题是,它们会被搜索引擎和其他自动系统索引。谷歌曾经有一个产品,它可以在你正在浏览的页面上预取链接,所以如果你点击这些链接,它们会更快地加载。它对那些有delete.php之类链接的网站造成了严重破坏。Id =1 -人们失去了他们的整个网站。