如何设置传输编码为分块,明示或暗示,在ASP.NET回应?

编程入门 行业动态 更新时间:2024-10-24 18:17:49
本文介绍了如何设置传输编码为分块,明示或暗示,在ASP.NET回应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以简单地设置了传输编码头?

在调用 Response.Flush()在某种程度上导致这种情况发生的隐?

修改结果不,我不能叫 Response.Headers.Add(传输编码,什么); 抛出。

任何其他建议?

  

相关报道:结果  启用ASP.NET 块传输编码

解决方案

的 TL; DR:的指定内容长度是实现一个快速的第一个字节的最好方式;你会允许在TCP分块而不是HTTP水平。如果你不知道内容长度,设置 context.Response.BufferOutput 到假,因为它是将发送输出使用传输编码分块写入的输出流。

你为什么要设置传输编码:分块?分块传送基本上是一个变通,允许发送文件的内容长度事先是不知道。 ASP.NET,但是,在默认情况下缓存整个输出,因此确实的知道整体内容的长度。

当然,HTTP是分层通过TCP,场面TCP被分裂甚至单片HTTP响应转换成数据包组块反正后面 - 这意味着如果您指定的内容长度达阵和禁用输出缓冲,你'会得到最好的潜伏期的没有的要求HTTP级分块。 因此,您不需要HTTP级分块当你知道内容长度提供了快速的第一个字节。

虽然我不是HTTP的专家,我实现了一个简单的流媒体服务器与寻求支持,动态COM pression,缓存等,以及我有一个快速的第一个字节的相关性合理把握 - 和分块一般是劣质选项的如果的你知道内容的长度 - 这是几乎可以肯定,为什么ASP.NET不会让你手动设置 - 它只是没有必要

然而,如果你的不的知道HTTP内容长度之前传输和缓冲太贵,关闭输出缓冲和presumably服务器将必然采用分块传输编码

何时服务器采用分块传输编码?的我只是测试,确实如 context.Response.BufferOutput 设置为假,当没有设置内容长度,响应分块;这种反应在内容编码一个1.7MB的我完全非科学的快速测试更大的1-2%:gzip的XML文档。由于gzip的依赖于上下文来减少冗余,我想象的COM pression比遭受更多,但似乎分块并不必然大大减少COM pression比率。

如果你看看在反射框架code,似乎在传送编码确实根据需要自动设置 - 如果缓冲是关闭的,没有内容的长度是已知的响应是一个HTTP / 1.1请求,即,分块传输编码使用。但是,如果服务器IIS7,这是工人的要求,code分支机构,一个本地方法(集成模式?) - 可能与相同的行为,但我不能验证

Can I simply set the Transfer-Encoding header?

Will calling Response.Flush() at some point cause this to occur implicitly?

EDIT No, I Cannot call Response.Headers.Add("Transfer-Encoding","anything"); That throws.

any other suggestions?

Related: Enable Chunked Transfer Encoding in ASP.NET

解决方案

TL;DR: Specifying the content-length is the best way to achieve a fast first byte; you'll allow chunking at TCP rather than HTTP level. If you don't know the content-length, setting context.Response.BufferOutput to false will send output as it's written the the output stream using chunked transfer-encoding.

Why do you want to set Transfer-Encoding: chunked? Chunked transfers are essentially a work-around to permit sending documents whose content-length is not known in advance. ASP.NET, however, by default buffers the entire output and hence does know the overall content length.

Of course, HTTP is layered over TCP, and behind the scene TCP is "chunking" anyhow by splitting even a monolithic HTTP response into packets - meaning that if you specify the content-length up front and disable output buffering, you'll get the best latency without requiring HTTP-level chunking. Thus, you don't need HTTP-level chunking to provide a fast first byte when you know the content-length.

Although I'm not an expert on HTTP, I have implemented a simple streaming media server with seeking support, dynamic compression, caching etc. and I do have a reasonable grasp of the relevance of a fast first byte - and chunking is generally an inferior option if you know the content-length - which is almost certainly why ASP.NET won't let you set it manually - it's just not necessary.

However, if you don't know the HTTP content length before transmission and buffering is too expensive, you turn off output buffering and presumably the server will use a chunked transfer encoding by necessity.

When does the server use chunked transfer encoding? I just tested, and indeed if context.Response.BufferOutput is set to false, and when the content length is not set, the response is chunked; such a response is 1-2% larger in my entirely non-scientific quick test of a 1.7MB content-encoding: gzip xml document. Since gzip relies on context to reduce redundancy, I'd expected the compression ratio to suffer more, but it seems that chunking doesn't necessarily greatly reduce compression ratios.

If you look at the framework code in reflector, it seems that the transfer encoding is indeed set automatically as needed - i.e. if buffering is off AND no content length is known AND the response is to an HTTP/1.1 request, chunked transfer encoding is used. However, if the server is IIS7 and this is a worker request (?integrated mode?), the code branches to a native method - probably with the same behavior, but I can't verify that.

更多推荐

如何设置传输编码为分块,明示或暗示,在ASP.NET回应?

本文发布于:2023-11-14 04:45:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586254.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:暗示   如何设置   NET   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!