Reprinted -get, post nature

GET and POST requests basic difference between the method of

GET and POST are two basic methods of the HTTP request, to say the difference between them, contact WEB development people can say one or two.

 

The most intuitive difference is that the GET parameters in the URL, POST body parameters passed by request.

 

You may have written their own numerous GET and POST requests, or have seen many authoritative sites summed up their differences, you very well know when to use.

 

When you asked this question in an interview, your heart is full of confidence and joy.

You easily given a "standard answer":
 

  • GET is harmless when the browser is rolled back and the POST request will be submitted again.

     

  • GET URL address can be generated Bookmark, and not POST.

     

  • GET requests are active cache browser, POST will not, unless manually.

     

  • GET request can only be url encoded, and POST supports multiple encoding.

     

  • GET request parameters are intact in the browser history, and the POST parameters will not be retained.

     

  • GET request parameters passed in the URL length is limited, but what there is POST.

     

  • The data type of the parameter, GET accepts only ASCII characters, but there is no limit POST.

     

  • GET more secure than POST, because the parameters directly exposed on the URL, it can not be used to transmit sensitive information.

     

  • GET parameters passed via the URL, POST Request body in place.

(This reference standard answer from w3schools)

 

"Unfortunately, this is not the answer we want!"

Please tell me the truth. . .

 

What if I told you there is no difference you on GET and POST nature believe it? 


Let's ripped coat GET and POST, candor it!


What GET and POST are? Two kinds of transmission protocol HTTP request method.

 

HTTP is what? HTTP is based on TCP / IP protocol on how to communicate data on how the World Wide Web.

 

HTTP is the underlying TCP / IP. So the bottom GET and POST is TCP / IP, that is to say, GET / POST is a TCP connection. GET and POST can do is the same as the same. Plus you give GET request body, to bring POST url parameter, technically completely do the same in. 

 

So, those differences "standard answer" in how it is?

 

 

在我大万维网世界中,TCP就像汽车,我们用TCP来运输数据,它很可靠,从来不会发生丢件少件的现象。但是如果路上跑的全是看起来一模一样的汽车,那这个世界看起来是一团混乱,送急件的汽车可能被前面满载货物的汽车拦堵在路上,整个交通系统一定会瘫痪。为了避免这种情况发生,交通规则HTTP诞生了。HTTP给汽车运输设定了好几个服务类别,有GET, POST, PUT, DELETE等等,HTTP规定,当执行GET请求的时候,要给汽车贴上GET的标签(设置method为GET),而且要求把传送的数据放在车顶上(url中)以方便记录。如果是POST请求,就要在车上贴上POST的标签,并把货物放在车厢里。当然,你也可以在GET的时候往车厢内偷偷藏点货物,但是这是很不光彩;也可以在POST的时候在车顶上也放一些数据,让人觉得傻乎乎的。HTTP只是个行为准则,而TCP才是GET和POST怎么实现的基本。

 

但是,我们只看到HTTP对GET和POST参数的传送渠道(url还是requrest body)提出了要求。“标准答案”里关于参数大小的限制又是从哪来的呢?

 


 

在我大万维网世界中,还有另一个重要的角色:运输公司。不同的浏览器(发起http请求)和服务器(接受http请求)就是不同的运输公司。 虽然理论上,你可以在车顶上无限的堆货物(url中无限加参数)。但是运输公司可不傻,装货和卸货也是有很大成本的,他们会限制单次运输量来控制风险,数据量太大对浏览器和服务器都是很大负担。业界不成文的规定是,(大多数)浏览器通常都会限制url长度在2K个字节,而(大多数)服务器最多处理64K大小的url。超过的部分,恕不处理。如果你用GET服务,在request body偷偷藏了数据,不同服务器的处理方式也是不同的,有些服务器会帮你卸货,读出数据,有些服务器直接忽略,所以,虽然GET可以带request body,也不能保证一定能被接收到哦。

 

好了,现在你知道,GET和POST本质上就是TCP链接,并无差别。但是由于HTTP的规定和浏览器/服务器的限制,导致他们在应用过程中体现出一些不同。 

 

你以为本文就这么结束了?

 

我们的大BOSS还等着出场呢。。。

 

这位BOSS有多神秘?当你试图在网上找“GET和POST的区别”的时候,那些你会看到的搜索结果里,从没有提到他。他究竟是什么呢。。。

 

GET和POST还有一个重大区别,简单的说:

GET产生一个TCP数据包;POST产生两个TCP数据包。

 

长的说:

对于GET方式的请求,浏览器会把http header和data一并发送出去,服务器响应200(返回数据);

而对于POST,浏览器先发送header,服务器响应100 continue,浏览器再发送data,服务器响应200 ok(返回数据)。

 

也就是说,GET只需要汽车跑一趟就把货送到了,而POST得跑两趟,第一趟,先去和服务器打个招呼“嗨,我等下要送一批货来,你们打开门迎接我”,然后再回头把货送过去。

 

因为POST需要两步,时间上消耗的要多一点,看起来GET比POST更有效。因此Yahoo团队有推荐用GET替换POST来优化网站性能。但这是一个坑!跳入需谨慎。为什么?

1. GET与POST都有自己的语义,不能随便混用。

2. 据研究,在网络环境好的情况下,发一次包的时间和发两次包的时间差别基本可以无视。而在网络环境差的情况下,两次包的TCP在验证数据包完整性上,有非常大的优点。

3. 并不是所有浏览器都会在POST中发送两次包,Firefox就只发送一次。

 

现在,当面试官再问你“GET与POST的区别”的时候,你的内心是不是这样的?

结束!!!

Guess you like

Origin blog.csdn.net/weixin_42479155/article/details/90239370