POST vs GET methods - at the network level, which is more impacted by latency?

吃可爱长大的小学妹 提交于 2019-12-11 03:07:41

问题


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?


回答1:


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:

00907f8252f7001e4fe86a93080045000028
0bb2400080067380ac100167adc22064c51a
01bb66ccad148448d84850103f05bde90000

And this is a POST request over SSL:

00907f8252f7001e4fe86a93080045000028
0c0640008006732cac100167adc22064c511
01bbe538c0df8621dc6150104042248c0000

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.




回答2:


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):

  • Generally web servers, like Apache, log the requests somewhere
  • 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

  1. As defined by w3 on HTTP/1.1, the GET has the ability to perform a partial request, thus limiting the network bandwidth:

    The semantics of the GET method change to a "partial GET" if the request message includes a Range header field. A partial GET requests that only part of the entity be transferred, as described in section 14.35. The partial GET method is intended to reduce unnecessary network usage by allowing partially-retrieved entities to be completed without transferring data already held by the client.

  2. Additionally, w3 describes a conditional GET to reduce network usage:

    The semantics of the GET method change to a "conditional GET" if the request message includes an If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, or If-Range header field. A conditional GET method requests that the entity be transferred only under the circumstances described by the conditional header field(s). The conditional GET method is intended to reduce unnecessary network usage by allowing cached entities to be refreshed without requiring multiple requests or transferring data already held by the client.

  3. GET requests are cacheable. This reduces network bandwidth because a browser may see a request that has already been made in the past, which isn't expired yet and responds by a seemingly instantaneous response from cache.



回答3:


For a given piece of data, they will likely be very nearly the same. Here is what a GET request may look like:

GET /test?x=5&y=3&z=4 HTTP/1.1
Header1: value
Header2: value
...

Here is how the same would look as a POST:

POST /test HTTP/1.1
Header1: value
Header2: value
...

x=5&y=3&z=4

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).




回答4:


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.




回答5:


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:

<form method="GET" action="/form-handler.aspx">
<input type="hidden" value="12345" />
<input type="submit" value="click to submit" />
</form>

Here is the POST version:

<form method="POST" action="/form-handler.aspx">
<input type="hidden" value="12345" />
<input type="submit" value="click to submit" />
</form>



回答6:


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.



来源:https://stackoverflow.com/questions/3892661/post-vs-get-methods-at-the-network-level-which-is-more-impacted-by-latency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!