简体   繁体   English

IIS 7.5 ASP.NET HttpModule-设置Response.Filter会导致分块编码

[英]IIS 7.5 ASP.NET HttpModule - Setting Response.Filter results in chunked encoding

I'm trying to create a HttpModule that changes the Response.Filter like so (for this demonstration just set the filter back to itself): 我正在尝试创建一个像这样更改Response.Filter的HttpModule(对于本演示,只需将过滤器设置回自身即可):

public class ContentTrafficMonitor : IHttpModule
{
  public void Init( HttpApplication context )
  {
     context.BeginRequest += OnBeginRequest;
  }

  public void Dispose()
  {
  }

  private static void OnBeginRequest( object sender, EventArgs e )
  {
     var application = (HttpApplication) sender;
     application.Response.Filter = application.Response.Filter;
  }

} }

Doing so sets the transfer encoding of the response to chunked, rather than using the Content-Length header. 这样做会将响应的传输编码设置为分块,而不是使用Content-Length标头。

If I remove the line where the Response.Filter is set, the response does have the Content-Length header. 如果删除设置了Response.Filter的行,则响应的确具有Content-Length标头。 Our application depends on the Content-Length header, is there any way to prevent this behavior? 我们的应用程序取决于Content-Length标头,有什么方法可以防止这种行为?

My guess is, setting the filter interferes with the normal buffering of the output, hence the output is now chunked. 我的猜测是,设置过滤器会干扰输出的正常缓冲,因此现在将输出分块。

Maybe you could imitate the behaviour by having your filter read till end, ie get all the output & set the content length header based on what you've read before you write everything read out. 也许可以通过读取过滤器直到结束来模仿这种行为,即获取所有输出并根据写入的内容在读取所有内容之前根据读取的内容设置内容长度标头。

It's only a guess though I'm afraid. 恐怕只是个猜测。

Simon 西蒙

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM