未知内容长度的 HTTP 标头
我目前正在尝试在转码过程后将内容流式传输到网络。通过将二进制文件写入我的网络流,通常可以正常工作,但某些浏览器(特别是 IE7、IE8)不喜欢在 HTTP 标头中定义 Content-Length。我相信“有效”标头应该具有此设置。
当您的内容长度未知时,将内容流式传输到网络的正确方法是什么?转码过程可能需要一段时间,因此我想在完成后开始将其流式传输。
I am currently trying to stream content out to the web after a trans-coding process. This usually works fine by writing binary out to my web stream, but some browsers (specifically IE7, IE8) do not like not having the Content-Length defined in the HTTP header. I believe that "valid" headers are supposed to have this set.
What is the proper way to stream content to the web when you have an unknown Content-Length? The trans-coding process can take awhile, so I want to start streaming it out as it completes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将它们与
Transfer-Encoding:< 一起发送/code>
分块
。更多详细信息请参见维基百科。更新根据评论,这里有一个例子,Java中的“ChunkedOutputStream”可能看起来像这样:
...基本上可以用作:
您在源代码中看到,每个数据块都存在一个header 表示十六进制数据的长度、一个 CRLF、实际数据和一个 CRLF。流的结尾由表示 0 长度的标头和两个 CRLF 表示。
注意:尽管有这个示例,但实际上您在基于 JSP/Servlet 的 Web 应用程序中不需要需要它。只要响应中未设置内容长度,Web 容器就会自动以块的形式传输它们。
Try sending them in chunks along with
Transfer-Encoding:
chunked
. More details in wikipedia.Update as per the comments, here's an example how a "ChunkedOutputStream" in Java may look like:
...which can basically be used as:
You see in the source, every chunk of data exist of a header which represents the length of data in hex, a CRLF, the actual data and a CRLF. The end of the stream is represented by a header denoting a 0 length and two CRLFs.
Note: despite the example, you actually do not need it in a JSP/Servlet based webapplication. Whenever the content length is not set on a response, the webcontainer will automatically transfer them in chunks.
作为 BalusC 优秀帖子的后续,这里是我在 C# 中使用的代码。从进程上的 STDOUT 接收数据后,我将数据手动直接分块到 HTTP 输出流。
Just as a follow up to BalusC's excellent post, here is the code I am using in C#. I am chunking data manually directly to an HTTP output stream, after receiving data from the STDOUT on a process.