HTTP keep-alive:修订间差异
第2行: | 第2行: | ||
= Implementations == | = Implementations == | ||
== HTTP 1. | == HTTP 1.1 == | ||
HTTP1.0 通过在请求头中添加Connection: keep- | HTTP1.0 通过在请求头中添加Connection: keep-alive启用长连接(非官方做法),而HTTP 1.1默认使用Keep-Alive机制(也叫持久化连接),可以使得多个HTTP请求共用一个底层的TCP连接。 | ||
正常情况下,客户端发送单个HTTP请求后,会发送RST报文主动断开连接: | 正常情况下,客户端发送单个HTTP请求后,会发送RST报文主动断开连接: | ||
2023年12月6日 (三) 10:09的版本
HTTP persistent connection, also called HTTP keep-alive, or HTTP connection reuse, is the idea of using a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair. The newer HTTP/2 protocol uses the same idea and takes it further to allow multiple concurrent requests/responses to be multiplexed over a single connection.
Implementations =
HTTP 1.1
HTTP1.0 通过在请求头中添加Connection: keep-alive启用长连接(非官方做法),而HTTP 1.1默认使用Keep-Alive机制(也叫持久化连接),可以使得多个HTTP请求共用一个底层的TCP连接。 正常情况下,客户端发送单个HTTP请求后,会发送RST报文主动断开连接:
如果不启用Keep-Alive或者因某种原因没有生效(比如Go语言中没有读取完Body内容时,该连接无法复用),发送多个HTTP请求时会导致多次握手建立连接:
而启用Keep-Alive后,发送多次HTTP请求无需重新握手:
当客户端Keep-Alive超时时间小于服务器超时时间时,会发送Keep-Alive报文(ACK):
当服务器Keep-Alive超时时间小于客户端超时时间时,则会主动发送FIN断开连接。再次发送数据时,需要重新握手建立连接: