据我所知,有三种类型:
不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。
我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?
据我所知,有三种类型:
不要使用GET和POST 不要使用POST而使用GET 你用哪一个都不重要。
我对这三种情况的假设正确吗?如果有,每个案例都有哪些例子?
当前回答
HTTP Post数据对数据量没有指定的限制,因为不同的浏览器对GET有不同的限制。RFC 2068声明:
服务器应谨慎 取决于URI长度大于255 字节,因为一些旧的客户端或 代理实现可能不正确 支持这些长度
具体来说,您应该根据它们的用途选择正确的HTTP结构。HTTP GET不应该有副作用,可以被HTTP代理等安全地刷新和存储。
当您想要针对url资源提交数据时,使用HTTP POST。
使用HTTP GET的一个典型示例是在搜索中,即搜索?查询=我+查询 使用HTTP POST的一个典型示例是向在线表单提交反馈。
其他回答
短的版本
GET:通常用于已提交的搜索请求,或者任何您希望用户能够再次调出确切页面的请求。
GET的优点:
url可以安全地添加书签。 页面可以安全地重新加载。
GET的缺点:
变量作为名称-值对通过url传递。(安全风险) 可以传递的变量数量有限。(基于浏览器。例如,Internet Explorer被限制为2048个字符。)
POST:用于更高安全性的请求,其中数据可能用于更改数据库,或者您不希望别人添加书签的页面。
POST的优点:
url中不显示名称-值对。(安全+= 1) 可以通过POST传递无限数量的名称-值对。参考。
POST的缺点:
使用POST数据的页面不能被书签。(如果你愿意的话。)
完整版
直接来自超文本传输协议——HTTP/1.1:
9.3 GET The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process. The semantics of the GET method change to a "conditional GET" if the request message includes an If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, or If-Range header field. A conditional GET method requests that the entity be transferred only under the circumstances described by the conditional header field(s). The conditional GET method is intended to reduce unnecessary network usage by allowing cached entities to be refreshed without requiring multiple requests or transferring data already held by the client. The semantics of the GET method change to a "partial GET" if the request message includes a Range header field. A partial GET requests that only part of the entity be transferred, as described in section 14.35. The partial GET method is intended to reduce unnecessary network usage by allowing partially-retrieved entities to be completed without transferring data already held by the client. The response to a GET request is cacheable if and only if it meets the requirements for HTTP caching described in section 13. See section 15.1.3 for security considerations when used for forms. 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.
使用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是一个有问题的设计,原因有很多,其中之一就是在按下后退按钮时出现烦人的“您必须重新提交这个表单,您希望这样做吗?”
当我不希望人们看到QueryString或当QueryString变大时,我使用POST。另外,文件上传需要POST。
不过,我不认为使用GET有问题,我将它用于简单的事情,其中将内容保留在QueryString上是有意义的。
使用GET将允许链接到POST无法工作的特定页面。
阅读维基百科上关于HTTP的文章。它将解释协议是什么以及它的作用:
得到 请求指定资源的表示形式。请注意,GET不应该用于导致副作用的操作,例如在web应用程序中使用它来执行操作。其中一个原因是,GET可以被机器人或爬虫任意使用,它们不需要考虑请求应该引起的副作用。
and
帖子 向已识别的资源提交要处理的数据(例如,来自HTML表单)。数据包含在请求体中。这可能导致创建新资源或更新现有资源,或者两者都有。
W3C有一个名为uri、Addressability的文档,以及HTTP GET和POST的使用,它解释了什么时候使用什么。引用
1.3 HTTP GET / POST选择快速检查 在以下情况下使用GET: 这种互动更像是一个问题(也就是说,它是一个问题) 安全操作,如查询、读取操作或查找)。
and
在以下情况下使用POST: 这种互动更像是一种命令,或者 交互以用户可以感知的方式改变资源的状态(例如,对服务的订阅),或者 o用户对互动的结果负责。 但是,在最终决定使用HTTP GET还是POST之前,还请考虑敏感数据和实际考虑因素。
一个实际的例子是每当您提交HTML表单时。您可以为表单操作指定post或get。PHP将相应地填充$_GET和$_POST。