没有内容长度标头,是否可以进行HTTP保持活动连接? [英] Are HTTP keep-alive connections possible without content-length headers?
问题描述
我了解在HTTP 1.0中,通过关闭连接.
I understand that in HTTP 1.0, the content of a response is terminated by closing the connection.
在HTTP 1.1中,引入了保持活动连接,从而启用了在一个TCP连接中有多个请求和响应.
In HTTP 1.1, keep-alive connections were introduced, enabling multiple requests and responses in a single TCP connection.
通过同一连接发送多条消息时,需要一种机制来定义一条消息的结束位置和下一条消息的开始位置.
When multiple messages are sent over the same connection, there needs to be a mechanism that defines where one message ends and the next message starts.
通过测试,我发现当设置 content-length时,此方法有效标头.通过了解内容的长度,客户端可以知道何时完全接收到内容,并可以解析下一个响应.
By testing, I found out that this works when I set the content-length header in a response. By knowing the content length, the client knows when the content is fully received and can parse the next response.
我的问题是:
是否可以在保持连接状态下发送多个响应而无需设置content-length标头?
如果是,怎么办?
为澄清起见:我正在考虑以下情况:开始将响应发送给客户端时,响应的长度未知,我想知道关闭连接是否是实现该请求的唯一方法.
For clarification: I am thinking about scenarios where the length of the response is not known when starting to send it to the client and I would like to know if closing the connection is the only way to implement that.
推荐答案
传输-Encoding 标头就是我想要的.
通过将传输编码设置为chunked
,可以省略Content-Length标头.
By setting the transfer-encoding to chunked
, it is possible to omit the Content-Length header.
在分块传输编码中,可以以长度已知的多个分块发送消息.要终止消息,将发送长度为零的块.
In the chunked transfer encoding, a message can be sent in multiple chunks for which the length is known. To terminate a message, a chunk with length zero is sent.
这样就可以建立保持活动状态的连接,并在开始发送消息时仍会发送长度未知的消息.
This makes it possible to have a keep-alive connection and still send messages where the length is unknown when starting to send them.
这篇关于没有内容长度标头,是否可以进行HTTP保持活动连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!