内容长度和http压缩困境
我的 ASP.NET 应用程序向用户返回 JSON 对象,它包含二进制编码数据。因此,我决定启用 HTTP 压缩,问题始于 Content-Length。
如果我启用压缩,则在发送响应且连接不会立即关闭时,内容长度标头将被忽略。所有数据发送完毕后,连接仍保持打开状态约 15 秒。
我想启用 HTTP 压缩,但不知道如何解决 Content-Length 标头的问题。
context.Response.AddHeader("Content-Length", jsonObject.ToString().Length.ToString());
context.Response.Write(jsonObject);
context.Response.Flush();
context.Response.Close();
My ASP.NET app returns JSON object to user, it contains binary encoded data. Due to this I decided to enable HTTP compression and the problem begun with Content-Length.
If I enable compression the Content-Length header is ignored while response s send and connection is not closed immediately. The connection is still open for about 15 seconds after all data has been sent.
I would like to have HTTP compression enabled but don't know how to solve the problem with Content-Length header.
context.Response.AddHeader("Content-Length", jsonObject.ToString().Length.ToString());
context.Response.Write(jsonObject);
context.Response.Flush();
context.Response.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Content-Length
表示在您的情况下传输的数据的长度,它是压缩字节。请参阅此问题,其答案链接到相关 RFC:使用 http 压缩时的内容长度如果 HTTP 压缩是通过 Web 服务器(而不是您的代码)进行的,那么我建议您尝试自行添加内容长度标头。希望网络服务器能够正确添加它。
如果您查看开发人员,可以使用 http://httpd.apache.org/ 上的 Chrome 来验证这一点在控制台中,您会看到
Content-Length
比实际未压缩的页面(以字节为单位)小得多。Content-Length
represents the length of the data being transferred in your case it is the compressed bytes. See this SO question whose answer links to relevant RFC: content-length when using http compressionIn case, HTTP Compression is happening via web server (and not by your code) then I will suggest you to try adding content-length header by your self. Hopefully, web server will add it correctly.
This can be verified using Chrome on http://httpd.apache.org/ if you look at the developer console you would see the
Content-Length
to be much smaller than the actual uncompressed page in bytes.