问题描述
HTTP 1.0 HTTP 1.0规范并未真正深入研究
Keep-Alive应该有效。基本上,支持Keep-Alive
的浏览器会在请求中添加一个额外的标头:
HTTP 1.0 The HTTP 1.0 specification does not really delve into how Keep-Alive should work. Basically, browsers that support Keep-Alive appends an additional header to the request as:
连接:保持活动当服务器处理请求时,
生成响应,它还为响应添加了一个标头:
连接:保持活动完成此操作后,套接字连接是
未像以前那样关闭,但在发送响应后保持打开状态。当
客户端发送另一个请求时,它会重用相同的连接。
连接将继续重复使用,直到客户端或
服务器确定对话结束,其中一个连接断开连接。
Connection: Keep-Alive When the server processes the request and generates a response, it also adds a header to the response: Connection: Keep-Alive When this is done, the socket connection is not closed as before, but kept open after sending the response. When the client sends another request, it reuses the same connection. The connection will continue to be reused until either the client or the server decides that the conversation is over, and one of them drops the connection.
以上说明来自此处。但是我不明白一件事
The above explanation comes from here. But I don't understand one thing
据我所知,我们只是发送tcp数据包来发出请求和响应,这是怎么回事套接字连接
有帮助,它是如何工作的?我们仍然需要发送数据包,但它怎么能以某种方式建立持久连接?这看起来很不真实。
As I understand we just send tcp packets to make requests and responses, how this socket connection
helps and how does it work? We still have to send packets, but how can it somehow establish the persistent connection? It seems so unreal.
推荐答案
建立新的TCP连接存在开销(DNS查询,TCP握手,SSL / TLS握手)等)。如果没有保持活动状态,则每个HTTP请求都必须建立新的TCP连接,然后在发送/接收响应后关闭连接。 keep-alive允许将现有TCP连接重用于多个请求/响应,从而避免所有这些开销。这就是使连接持久的原因。
There is overhead in establishing a new TCP connection (DNS lookups, TCP handshake, SSL/TLS handshake, etc). Without a keep-alive, every HTTP request has to establish a new TCP connection, and then close the connection once the response has been sent/received. A keep-alive allows an existing TCP connection to be re-used for multiple requests/responses, thus avoiding all of that overhead. That is what makes the connection "persistent".
在HTTP 0.9和1.0中,默认情况下,服务器在向客户端发送响应后关闭其TCP连接的结束。客户端必须在收到响应后关闭其TCP连接的末尾。在HTTP 1.0(但不是0.9)中,客户端可以通过在请求中包含 Connection:keep-alive
标头,明确要求服务器不要关闭其连接的结尾。如果服务器同意,它在响应中包含 Connection:keep-alive
标头,并且不会关闭其连接的结尾。然后,客户端可以重新使用相同的TCP连接来发送其下一个请求。
In HTTP 0.9 and 1.0, by default the server closes its end of a TCP connection after sending a response to a client. The client must close its end of the TCP connection after receiving the response. In HTTP 1.0 (but not in 0.9), a client can explicitly ask the server not to close its end of the connection by including a Connection: keep-alive
header in the request. If the server agrees, it includes a Connection: keep-alive
header in the response, and does not close its end of the connection. The client may then re-use the same TCP connection to send its next request.
在HTTP 1.1中, keep-alive
是默认行为,除非客户端明确要求服务器通过在其请求中包含 Connection:close
标头来关闭连接,否则服务器决定包含连接:在其响应中关闭
标题。
In HTTP 1.1, keep-alive
is the default behavior, unless the client explicitly asks the server to close the connection by including a Connection: close
header in its request, or the server decides to includes a Connection: close
header in its response.
这篇关于http keep-alive如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!