当前位置:网站首页>小小面试题之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。
边栏推荐
- 两步处理字符串正则匹配得到JSON列表
- Codeforces Round #802 (Div. 2)(A-D)
- 【活动推荐】云原生、产业互联网、低代码、Web3、元宇宙……哪个是 2022 年架构热点?...
- cartographer_ pose_ graph_ 2d
- cartographer_ local_ trajectory_ builder_ 2d
- Mise en œuvre du routage dynamique par zuul
- C# 40. Byte[] to hexadecimal string
- LSTM in tensorflow_ Layers actual combat
- tensorlow:cifar100_ train
- Zuul 實現動態路由
猜你喜欢
![C# 39. Conversion between string type and byte[] type (actual measurement)](/img/33/046aef4e0c1d7c0c0d60c28e707546.png)
C# 39. Conversion between string type and byte[] type (actual measurement)

localStorage浏览器本地储存,解决游客不登录的情况下限制提交表单次数。

cartographer_pose_graph_2d

Red team scoring method statistics

Classic theory: detailed explanation of three handshakes and four waves of TCP protocol

86. (cesium chapter) cesium overlay surface receiving shadow effect (gltf model)

Mongodb image configuration method

cartographer_ optimization_ problem_ 2d

Tp5.0 framework PDO connection MySQL error: too many connections solution

cartographer_fast_correlative_scan_matcher_2d分支定界粗匹配
随机推荐
Collections and dictionaries
5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)
Machine learning final exercises
Windows下安装Tp6.0框架,图文。Thinkphp6.0安装教程
localStorage浏览器本地储存,解决游客不登录的情况下限制提交表单次数。
【Unity3D】碰撞体组件Collider
cartographer_backend_constraint
[greedy college] Figure neural network advanced training camp
Yunqi lab recommends experience scenarios this week, free cloud learning
vscode config
cartographer_ pose_ graph_ 2d
How to ensure the efficiency and real-time of pushing large-scale group messages in mobile IM?
Implementation of IM message delivery guarantee mechanism (II): ensure reliable delivery of offline messages
Codeforces Round #802 (Div. 2)(A-D)
Final review of brain and cognitive science
百度API地图的标注不是居中显示,而是显示在左上角是怎么回事?已解决!
tensorlow:cifar100_ train
PHP one sentence Trojan horse
Codeforces Round #800 (Div. 2)
Install the tp6.0 framework under windows, picture and text. Thinkphp6.0 installation tutorial