GET 和 POST的区别

1、最普遍的答案

  GET使用URL或Cookie传参。而POST将数据放在BODY中。

  GET的URL会有长度上的限制,则POST的数据则可以非常大。

  POST比GET安全,因为数据在地址栏上不可见。

2、GET和POST与数据如何传递没有关系

  GET和POST是由HTTP协议定义的。在HTTP协议中,Method和Data(URL, Body, Header)是正交的两个概念,也就是说,使用哪个Method与应用层的数据如何传输是没有相互关系的。

  HTTP没有要求,如果Method是POST数据就要放在BODY中。也没有要求,如果Method是GET,数据(参数)就一定要放在URL中而不能放在BODY中。

  那么,网上流传甚广的这个说法是从何而来的呢?在HTML标准中,找到了相似的描述 ,如下:

  17.13.1 Form submission method

  The method attribute of the FORM element specifies the HTTP method used to send the form to the processing agent. This attribute may take two values:

  [1]get:With the HTTP "get" method, the form data set is appended to the URI specified by the action attribute (with a question-mark ("?") as separator) and this new URI is sent to the processing agent.

  [2] post:With the HTTP "post" method, the form data set is included in the body of the form and sent to the processing agent.

  The "get" method should be used when the form is idempotent (i.e., causes no side-effects). Many database searches have no visible side-effects and make ideal applications for the "get" method.

  If the service associated with the processing of a form causes side effects (for example, if the form modifies a database or subscription to a service), the "post" method should be used.

  Note. The "get" method restricts form data set values to ASCII characters. Only the "post" method (with enctype="multipart/form-data") is specified to cover the entire [ISO10646] character set.

  但是这只是HTML标准对HTTP协议的用法的约定。如果说成区别也仅仅是 HTML 标准下的区别。

  而且,现代的Web Server都是支持GET中包含BODY这样的请求。虽然这种请求不可能从浏览器发出,但是现在的Web Server又不是只给浏览器用,已经完全地超出了HTML服务器的范畴了。

3、HTTP协议对GET和POST都没有对长度的限制

  HTTP协议明确地指出了,HTTP头和Body都没有长度的要求。而对于URL长度上的限制,有两方面的原因造成:

  [1] 浏览器。据说早期的浏览器会对URL长度做限制。据说IE对URL长度会限制在2048个字符内(流传很广,而且无数同事都表示认同)。但我自己试了一下,我构造了90K的URL通过IE9访问live.com,是正常的。网上的东西,哪怕是Wikipedia上的,也不能信。

  [2] 服务器。URL长了,对服务器处理也是一种负担。原本一个会话就没有多少数据,现在如果有人恶意地构造几个几M大小的URL,并不停地访问你的服务器。服务器的最大并发数显然会下降。另一种攻击方式是,把告诉服务器Content-Length是一个很大的数,然后只给服务器发一点儿数据,嘿嘿,服务器你就傻等着去吧。哪怕你有超时设置,这种故意的次次访问超时也能让服务器吃不了兜着走。有鉴于此,多数服务器出于安全啦、稳定啦方面的考虑,会给URL长度加限制。但是这个限制是针对所有HTTP请求的,与GET、POST没有关系。

posted on 2016-09-07 09:54  Now,DayBreak  阅读(500)  评论(0编辑  收藏  举报