“响应过滤器的无效使用”压缩来自 IHttpHandler 的响应时

发布于 2024-10-14 09:47:47 字数 1159 浏览 7 评论 0原文

我有一个 IHttpHandler 返回一个文件。当响应流被压缩时,无论是自动使用 Telerik RadCompression 还是通过使用

context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);

返回到浏览器的响应显式设置过滤器都是正确的,但响应末尾是一些 HTML。 HTML 包含异常:

[HttpException (0x80004005): Invalid use of response filter]
   System.Web.HttpResponseStreamFilterSink.VerifyState() +3928894
   System.Web.HttpResponseStreamFilterSink.Write(Byte[] buffer, Int32 offset, Int32 count) +28
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +363
   System.IO.Stream.Close() +28
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +63
   System.IO.Stream.Close() +28
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +595
   System.IO.Stream.Close() +28
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +63
   System.IO.Stream.Close() +28
   System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +754
   System.Web.HttpResponse.FilterOutput() +121
   System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +174

如果我确保响应未压缩,则响应不包含异常。

这是怎么回事?

I have an IHttpHandler returning a file. When the response stream is compressed, either automatically using Telerik RadCompression or by explicitly setting a filter using

context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);

the response returned to the browser is correct but at the end of the response is some HTML. The HTML contains the exception:

[HttpException (0x80004005): Invalid use of response filter]
   System.Web.HttpResponseStreamFilterSink.VerifyState() +3928894
   System.Web.HttpResponseStreamFilterSink.Write(Byte[] buffer, Int32 offset, Int32 count) +28
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +363
   System.IO.Stream.Close() +28
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +63
   System.IO.Stream.Close() +28
   System.IO.Compression.DeflateStream.Dispose(Boolean disposing) +595
   System.IO.Stream.Close() +28
   System.IO.Compression.GZipStream.Dispose(Boolean disposing) +63
   System.IO.Stream.Close() +28
   System.Web.HttpWriter.FilterIntegrated(Boolean finalFiltering, IIS7WorkerRequest wr) +754
   System.Web.HttpResponse.FilterOutput() +121
   System.Web.CallFilterExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +174

If I make sure the response is NOT compressed, the response contains no exception.

What's up with that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

演出会有结束 2024-10-21 09:47:47

确保您没有阻止缓冲。显然,要压缩响应,您必须允许对其进行缓冲。

即,需要删除以下两行之一:

context.Response.BufferOutput = false;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);

Make sure you're not preventing buffering. Apparently, to compress the response, you have to allow it to be buffered.

I.e., one of the following two lines needs to be removed:

context.Response.BufferOutput = false;
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文