POST 与 GET 方法 - 在网络级别,哪个受延迟影响更大?
HTTP GET 和 POST 请求的网络通信有什么区别吗?
使用 GET,我知道整个请求是一次性发送的。 使用 POST,我认为发送了初始请求,然后发送了第二个请求,该请求发送了所有参数。
例如,假设服务器和客户端之间的延迟为 500ms。 GET 与 POST 调用的总时间是多少?
Is there any difference in network communication for HTTP GET and POST requests?
With GET, I understand that the entire request is sent in one go.
With POST, I think the initial request is sent, and then a second request is sent which sends all the parameters.
For example, assume that the latency between server and client is 500ms. What would be the total time for a GET vs POST call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
报文均为TCP报文,承载HTTP协议,请求方式不会改变网络层的响应时间。
它会根据请求的大小而根据请求的大小而变化,但这不是由请求类型决定的。
您可以通过 post 发送比 get 更多的数据,但这并不意味着它们响应速度更快,这是一个单独的问题。
HTTP 服务器处理和返回结果的速度取决于您正在使用的服务器,并且可能非常微不足道,不值得一提。
从服务器返回结果的速度取决于HTTP服务器正在处理什么资源,如果它调用需要时间的PHP文件,那么它将需要时间......
数据包没有明显的区别...这是通过 SSL 的 GET 请求:
这是通过 SSL 的 POST 请求:
对于 TCP 数据包内的字符串是“GET”还是“POST”,网络会查看,并没有太多涉及它说:“哦,你是 TCP,嗯?好吧,那你就走吧。”它不在乎。
正常网络流量之外的任何延迟都完全是由于服务器级别的处理或其正在运行的代码造成的。
The packets are all TCP packets, which carry the HTTP protocol, the request method doesn't change the network layer's response time.
It will change on request-to-request basis depending on the size of a request, but that's not determined by the request type.
You can send more data with post than get, but this doesn't mean they respond faster, it's a separate issue.
The speed at which the HTTP server can process and return a result is on the server you're using, and likely to be so negligible it's not worth mentioning.
The speed at which the result comes back from the server depends on what resource the HTTP server is processing, if it's calling a PHP file that takes time, then it's going to take time...
There isn't a clear difference in the packets... this is a GET request over SSL:
And this is a POST request over SSL:
There isn't exactly a lot of involvement of whether the string inside the TCP packet is "GET" or "POST," the network looks at it, says "oh, you're TCP, Huh? Well, off you go then." It doesn't care.
Any delay outside of normal network traffic is soley due to processing on the sever level, or on the code it's working through.
由于日志记录较少,POST 请求具有优势
给定相同数量的信息(POST 消息不超过 GET),POST 在技术上在服务器端应该更快(纳秒到皮秒):
POST 请求不会记录查询字符串,因此会减少写入处理。服务器 IOPS 可能会无意中影响延迟
如果没有这个,给定相同的数据包,它们实际上是等效的。
GET 将数据存储在查询字符串中,POST 将信息存储在消息正文中。
服务器处理两者,只是以不同的方式。
在客户端,POST 需要更多处理来准备消息。如果执行任何 AJAX,您会注意到这一点,发送 GET 请求比 POST 容易得多。
GET 的性能优于 POST 请求
正如 w3 在 HTTP/1.1 上所定义的,GET有能力执行部分请求,从而限制网络带宽:
<块引用>
如果请求消息包含 Range 标头字段,则 GET 方法的语义将更改为“部分 GET”。部分 GET 请求仅传输实体的一部分,如第 14.35 节所述。部分 GET 方法旨在通过允许完成部分检索的实体而不传输客户端已持有的数据来减少不必要的网络使用。
此外,w3 描述了条件 GET 以减少网络使用:
<块引用>
如果请求消息包含 If-Modified-Since、If-Unmodified-Since、If-Match、If-None-Match 或 If-Range 标头字段,则 GET 方法的语义更改为“条件 GET” 。条件 GET 方法请求仅在条件标头字段描述的情况下传输实体。条件 GET 方法旨在通过允许刷新缓存实体而不需要多个请求或传输客户端已持有的数据来减少不必要的网络使用。
POST requests have edge due to less logging
Given the same amount of information (the POST message doesn't exceed the GET), POST should technically be faster on the serverside (by nano-to-picoseconds):
POST requests do not log the query string, thus less write processing. Server IOPS may inadvertently affect latency
Without this, given the same packets, they're practically equivalent.
The GET stores data in the query string, the POST stores the info in the message body.
The server processes both, just in different ways.
On the client side, the POST requires more processing to prepare the message. You'll notice this if doing any AJAX, it's a lot easier to send a GET request than POST.
GET has the ability to outperform POST requests
As defined by w3 on HTTP/1.1, the GET has the ability to perform a partial request, thus limiting the network bandwidth:
Additionally, w3 describes a conditional GET to reduce network usage:
我在 Wireshark 中进行监控时对此进行了测试。
我创建了一个简单的 HTML 表单,并在 GET 和 POST 之间切换方法。
我始终注意到 GET 请求发送一个数据包,而 POST 发送两个数据包。
即使表单数据非常小,POST 数据也始终在第二个数据包中发送。
这对我来说意味着 POST 会更容易受到延迟的影响。
更新 2011.07.05:
这是用于 POST 的简单 HTML 表单:
这是 POST 版本:
I tested this while monitoring in Wireshark.
I created a simple HTML form and toggled the method between GET and POST.
Consistently, I noticed that GET requests send one packet, while POST sends two.
Even when the form data is very small, the POST data is always sent in the second packet.
This suggests to me that POST would be more impacted by latency.
UPDATE 2011.07.05:
Here is the simple HTML form for POST:
Here is the POST version:
对于给定的数据,它们可能非常接近相同。 GET 请求可能如下所示:
这是与 POST 请求相同的方式:
因此,数据量相同。真正的问题是您是否希望用户能够添加书签并返回到 URL 并在将来再次看到相同的数据。 GET 用于此目的,POST 用于请求在服务器上更改数据,或者出于安全原因(例如,不要使用 GET 提交密码)。
For a given piece of data, they will likely be very nearly the same. Here is what a GET request may look like:
Here is how the same would look as a POST:
So it's the same amount of data. The real question is whether you want the user to be able to bookmark and return to the URL and see the same data again in the future. GETs are used for that, POSTs are used for requesting data be changed on the server, or for security reasons (don't use GET to submit a password, for example).
乔纳森的回答非常明确。但让我进一步讨论请求彼此不同的地方。
所有通过互联网流动的信息都经过小包。假设每个包的最大容量为 1KB(这不是正确的值,它只是为了澄清机制,如果您想要有关限制的实际值,请在 RFC 上搜索)。
好的,我们有一个 GET 和一个 POST 请求。正如乔纳森所展示的,这些包非常相似。这样的话,在数据量较小的情况下,所有内容都可以封装在 1KB 的包内,因此网络性能上没有差异。
但是如果请求需要很大怎么办?有些人知道,但是 GET 请求有一个最大长度,该长度可能因服务器而异。尝试询问任意 site.com/foo/a{200 次 A}。它将返回无效/错误的请求,而不仅仅是 404 未找到。
所以这里就是 POST 发生的地方。如果数据量大于某个值,POST 允许服务器继续列出该请求并解析该值。
此外,行为上还存在之前未提及的另一个潜在差异。 POST 数据在发送到服务器之前在浏览器中解析为当前文档编码。
Jonathan's answer is very elucidative. But let me go a little further on the spot where requests differ from each other.
All information flowing through internet goes through small packages. Let's say each package has a max capacity of 1KB (this is not the correct value, it's just for clarification on the mechanism, if you want real values about limits go search on RFC's).
OK, so we have a GET and a POST request. The packages are very similar as ilustrated by Jonathan. In that case, with a small amount of data, everything can be wrapped inside the 1KB package, thus there are no differentce on the network performance.
But what if the request needs to be huge? A few people know, but there is a max length for a GET request which may vary by server. Try ask any site.com/foo/a{200 times A}. It will return an invalid/bad request, instead of just a 404 not found.
So here is where POST takes place. If data amount is bigger than a certain value, POST allows the server to continue listing to that request and parse the values.
Also there is another underlying difference in the behavior that wasn't mentioned before. POST data is parsed within browser to the current document encoding before being sent to the server.
这一切都与客户端的实现有关。在http规范中没有这样的条件。
发送时间取决于数据量。如果只用POST来代替GET,那就没有区别了。
Its all about implementation on client side. In http specs there is no such condition.
Time to send depends on amount of data. If you use POST only to replace GET it will be indistinguishably.