据我所知,有三种类型:
不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。
我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?
据我所知,有三种类型:
不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。
我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?
当前回答
使用POST进行破坏性操作,例如创建(我知道这很讽刺)、编辑和删除,因为您不能在浏览器的地址栏中点击POST操作。当允许一个人调用一个动作是安全的时候使用GET。URL是这样的:
http://myblog.org/admin/posts/delete/357
应该带你到一个确认页面,而不是简单地删除项目。这样避免事故要容易得多。
POST也比GET更安全,因为您没有将信息粘贴到URL中。因此,使用GET作为收集密码或其他敏感信息的HTML表单的方法并不是最好的主意。
最后一点注意:POST可以比GET传输更多的信息。“POST”没有传输数据的大小限制,而“GET”限制为2048个字符。
其他回答
另一个区别是POST通常需要两个HTTP操作,而GET只需要一个。
编辑:我应该澄清一下——对于常见的编程模式。一般来说,用一个直接的HTML网页来回复一个POST是一个有问题的设计,原因有很多,其中之一就是在按下后退按钮时出现烦人的“您必须重新提交这个表单,您希望这样做吗?”
我的一般经验法则是,当您向服务器发出不打算改变状态的请求时使用Get。post是为更改状态的服务器请求保留的。
来自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。
因为get是纯粹的url,它们可以被web浏览器缓存,并且可能更好地用于一致生成的图像之类的事情。(设置过期时间)
一个来自gravatar页面的例子:http://www.gravatar.com/avatar/4c3be63a4c2f539b013787725dfce802?d=monsterid
GET可能会产生稍微更好的性能,一些web服务器在调用处理程序之前将POST内容写入临时文件。
另一件需要考虑的事情是尺寸限制。get的上限是URL的大小,标准是1024字节,不过浏览器可能支持更多。
传输更多的数据应该使用POST来获得更好的浏览器兼容性。
即使小于这个限制也是一个问题,正如另一个帖子所写的,URL中的任何内容都可能最终出现在浏览器UI的其他部分,比如历史记录。