Content-Length 标头在 http 响应中有什么用?有必要传递标头吗?如果值大于或小于实际内容怎么办?

What is the use of Content-Length header in http response? It is necessary to pass the header? What if the value if more or less than actual content?

提问人:Prabal Aggarwal 提问时间:9/20/2023 更新时间:9/20/2023 访问量:20

问:

我想了解 Content-Length 标头的工作原理以及浏览器和服务器如何处理它。我看到过没有百分长度标题的回复,有些回复有内容长度。此外,我还看到响应的价值小于实际消息正文大小,并且浏览器会调整内容。我想知道有关标题的最佳实践。

HTML Apache HTTP 服务器

评论

0赞 Martheen 9/20/2023
规范在 RFC rfc-editor.org/rfc/rfc9110#field.content-length

答:

0赞 Brad 9/20/2023 #1

如今,您要么需要在标头中发送适当的响应,要么需要以分块传输编码发送响应。数据少于预期的 HTTP 响应是错误的。Content-Length