1. Concept
Keep-alive mode (also known as persistent connection, connection reuse), the Keep-alive feature keeps the client-to-server connection active, and keep-alive functionality avoids establishing or re-establishing a connection when a subsequent request to the server occurs.
HTTP 1.0 is turned off by default, you need to add "connection:keep-alive" in the HTTP header to enable keep-alive,keep-alive is enabled by default in HTTP 1.1, if you join "Connection: Close "before it is closed . Most browsers now use the http1.1 protocol, which means that the Keep-alive connection request is initiated by default, so whether a full keep-alive connection can be completed depends on the server setup.
2. How to determine the connection interruption
Description
In HTTP 1.0 and previous versions, the Content-length field is optional.
In http1.1 and later versions. If it is keep alive, then content-length and chunk must be two to choose one . If the non-keep alive, then the same as http1.0. Content-length is dispensable. If you have a transfer-encoding, you cannot have content-length.
(1) Content-length
Content-Length
Represents the length of the entity content . The browser uses this field to determine whether the currently requested data has been fully received .
Therefore, when a browser requests a static resource, that is, the server can explicitly know the length of the returned content, it can be set Content-Length
to control the end of the request. However, when the server does not know the length of the request result, such as a dynamic page or data, can Content-Length
not solve the above problem, this time need to use the Transfer-Encoding
field.
(2) transfer-encoding
The chunk code divides the data into a piece of the occurrence. The chunked encoding will be concatenated with a number of chunk, ending with a chunk marked with a length of 0 .
HTTP keep-alive Explanation