HTTP 响应结束 [英] End of an HTTP Response

查看:37
本文介绍了HTTP 响应结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么表示 HTTP 响应结束,没有 Content-Length 标头?

What indicates the end of an HTTP Response, without having a Content-Length header?

我已经读到,在某些情况下,该服务器会关闭 TCP 连接.但是我亲眼目睹了收到所有响应后TCP连接存在的情况.

I've read that in some case, that server closes the TCP connection. But I've witnessed cases that the TCP Connection existed after receiving all the response.

所以在以下情况下会出现一个主要问题:

So a major problem arises when:

  • 没有 Content-Length 标头
  • 收到所有 HTTP 响应后 TCP 连接存在
  • 浏览器希望使用相同的 TCP 连接来发送更多 HTTP 请求

在这种情况下,浏览器如何知道它可以使用相同的 TCP 连接?服务器可能会发送更多与第一个 HTTP 请求相关的内容,这些内容会与第二个 HTTP 响应混淆.

In that case, how the browser knows it can use the same TCP connection? The server might send more content that's relevant to the 1'st HTTP Request and that would get mixed up with the 2'nd HTTP Response.

推荐答案

如果客户端和服务端都支持HTTP 1.1,而服务端事先不知道响应体大小(因此无法发送Content-Length 响应头),服务器应该使用 chunkedencoding,即使服务器在发送响应后没有立即关闭 TCP 连接,客户端也可以找到响应正文的结尾.

If both the client and the server support HTTP 1.1, and the server does not know the response body size in advance (and therefore cannot send the Content-Length response header), the server should use chunked encoding, which allows the client to find the end of the response body even if the server does not close the TCP connection immediately after sending the response.

如果无法使用 HTTP 1.1(因为客户端或服务器太旧),则无法使用分块编码,在这种情况下发送没有 Content-Length 标头的响应会阻止服务器保持 TCP 连接持久.

If HTTP 1.1 cannot be used (because either the client or the server is too old), the chunked encoding cannot be used, and in such cases sending a response without the Content-Length header prevents the server from keeping the TCP connection persistent.

这篇关于HTTP 响应结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆