当前位置:网站首页>小小面试题之GET和POST的区别
小小面试题之GET和POST的区别
2022-06-26 05:17:00 【青铜大神】
这个看上去很简单的问题,背后关联着很多东西。因为面试的时候总爱问这种复杂的简单问题,所以我们今天就来一探究竟。
先说答案
功能不同:get用来获取数据,post用来提交表单。
发送数据量不同:get会将提交数据拼接到url的queryString中,而流量器对url的长度有限制,post发送数据会将数据放在body中传输,默认为不受限制。
安全性:由于get方法会将数据拼接在url上,所以用户可以直观的在浏览器中看到,而post的数据无法在浏览器中直观看到,所以安全性上post要强于get。
幂等性:get方法需要是幂等的,而post方法是非幂等的。
可缓存:由于get方法满足幂等性,所以get方法可以被缓存,而post方法不行。
功能不同
这个区别没有什么好说的,关于GET和POST的定义,在RFC7231规范中有说明。
GET定义部分
The GET method requests transfer of a current selected representation
for the target resource. GET is the primary mechanism of information
retrieval and the focus of almost all performance optimizations.
Hence, when people speak of retrieving some identifiable information
via HTTP, they are generally referring to making a GET request.
POST定义部分
The POST method requests that the target resource process the
representation enclosed in the request according to the resource's
own specific semantics. For example, POST is used for the following
functions (among others):
o Providing a block of data, such as the fields entered into an HTML
form, to a data-handling process;
o Posting a message to a bulletin board, newsgroup, mailing list,
blog, or similar group of articles;
o Creating a new resource that has yet to be identified by the
origin server;
o Appending data to a resource's existing representation(s)
大概意思就是GET请求用了来获取html\图片\css\js等资源文件;POST请求用来提交form表单。
幂等性和可缓存
RFC7231规范中同样定义了GET方法的幂等性和可缓存特性,而没有要求post。但毕竟谁也不愿自己的账号密码被浏览器缓存面临账号安全危险。
Safe Methods
Of the request methods defined by this specification, the GET, HEAD,OPTIONS, and TRACE methods are defined to be safe.
...
Cacheable Methods
Request methods can be defined as "cacheable" to indicate that responses to them are allowed to be stored for future reuse; for specific requirements see [RFC7234]. In general, safe methods that do not depend on a current or authoritative response are defined as cacheable; this specification defines GET, HEAD, and POST as cacheable, although the overwhelming majority of cache implementations only support GET and HEAD.
发送数据量不同
get请求是什么样的,往往都是taobao.com?item=111这种。将请求参数携带在url上。但是浏览器对url的长度有一定的限制,那么get请求就跟post请求所能携带的数据量产生一定差距。
有说法是2048个字符的限制,在知乎中有人回应了这个问题,我就摘抄过来。
作者:大宽宽
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
先说浏览器。不同浏览器不太一样。比如我们常说的2048个字符的限制,其实是IE8的限制。并且原始文档的说的其实是“URL的最大长度是2083个字符,path的部分最长是2048个字符“。见https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer。IE8之后的IE URL限制我没有查到明确的文档,但有些资料称IE 11的地址栏只能输入法2047个字符,但是允许用户点击html里的超长URL。我没实验,哪位有兴趣可以试试。
Chrome的URL限制是2MB,见https://chromium.googlesource.com/chromium/src/+/master/docs/security/url_display_guidelines/url_display_guidelines.md
Safari,Firefox等浏览器也有自己的限制,但都比IE大的多,这里就不挨个列出了。
然而新的IE已经开始使用Chrome的内核了,也就意味着“浏览器端URL的长度限制为2048字符”这种说法会慢慢成为历史。
安全性
由于get方法是将请求参数拼接在url中,所以安全性方面受到了考验。毕竟谁也不希望自己的账号和密码以这种方式呈现在流览器上taobao.com/login?username=xxx&pwd=xxx这也太没有安全感了对吧,你的隐私信息可以暴露在浏览器界面上。这点是不如post的。
当然了,这里谈论的安全性只是相对的,毕竟你都在使用http了,相当于你在裸泳,那暴不暴露,全看潮涨潮落。隐私信息还是要使用https。
边栏推荐
- app 应用安装到手机,不显示图标,引发的思考
- vscode config
- 程序人生
- CMakeLists. txt Template
- AD教程系列 | 4 - 创建集成库文件
- LeetCode 19. 删除链表的倒数第 N 个结点
- ssh连win10报错:Permission denied (publickey,keyboard-interactive).
- How does P2P technology reduce the bandwidth of live video by 75%?
- LSTM in tensorflow_ Layers actual combat
- ECCV 2020 double champion team, take you to conquer target detection on the 7th
猜你喜欢
Fedora alicloud source
Codeforces Round #800 (Div. 2)
Classic theory: detailed explanation of three handshakes and four waves of TCP protocol
LeetCode 19. 删除链表的倒数第 N 个结点
Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
Wechat team sharing: technical decryption behind wechat's 100 million daily real-time audio and video chats
[latex] error type summary (hold the change)
【Unity3D】刚体组件Rigidbody
LeetCode 19. Delete the penultimate node of the linked list
2. < tag dynamic programming and conventional problems > lt.343 integer partition
随机推荐
Protocol selection of mobile IM system: UDP or TCP?
Excellent learning ability is your only sustainable competitive advantage
The localstorage browser stores locally to limit the number of forms submitted when tourists do not log in.
PHP 2D / multidimensional arrays are sorted in ascending and descending order according to the specified key values
Gd32f3x0 official PWM drive has a small positive bandwidth (inaccurate timing)
关于支付接口回调地址参数字段是“notify_url”,签名过后的特殊字符url编码以后再解码后出现错误(¬ , ¢, ¤, £)
Happy New Year!
One of token passing between microservices @feign's token passing
86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)
CMakeLists. txt Template
ECCV 2020 double champion team, take you to conquer target detection on the 7th
[geek] product manager training camp
Douban top250
86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)
Anaconda creates tensorflow environment
SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor
Technical problems to be faced in mobile terminal im development
vscode config
CMakeLists.txt Template
Two step processing of string regular matching to get JSON list