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

查看:159
本文介绍了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 标题

  • 收到所有后,TCP连接存在HTTP响应

  • 浏览器希望使用相同的TCP连接以发送更多HTTP请求

  • There isn't a Content-Length header
  • TCP connection exist after receiving all of the HTTP Response
  • The browser wishes to use the same TCP connection in order to send more HTTP Requests

在这种情况下,浏览器如何知道它可以使用相同的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 响应标题),服务器应使用分块编码,即使服务器在发送响应后没有立即关闭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天全站免登陆