IIS 7管理模块无法获取发送内容长度或字节 [英] IIS 7 managed module can't get Content-Length or bytes sent

查看:157
本文介绍了IIS 7管理模块无法获取发送内容长度或字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有IIS 6 ISAPI筛选器使用响应的字节发送的领域,做一些定制加工。我想更新,对于IIS 7,但我遇到了一个问题。在IIS 7的事件没有人可以访问内容长度,发送字节,或将让我算算内容长度或字节的发送的任何数据。 (我知道Content-Length头和发送的字节数是不一样的,但无论是将为此努力。)

I have an ISAPI filter for IIS 6 which does some custom processing using the bytes-sent field of the response. I'd like to update that for IIS 7, but I'm running into a problem. None of the IIS 7 events seem to have access to the content-length, bytes sent, or any data which would let me calculate the content-length or bytes sent. (I know the content-length header and bytes sent are not the same, but either will work for this purpose.)

这是我所知道的,内容长度报头由HTTP.SYS的管理模块已执行完毕后添加。现在我有它运行在EndRequest事件处理程序。如果我能在输出流得到我可以计算一下,我需要我自己,但托管管道似乎不能够访问,要么。

From what I can tell, the content-length header is added by HTTP.SYS after the managed modules have finished executing. Right now I have an event handler which runs on EndRequest. If I could get at the output stream I could calculate what I need myself but the managed pipeline doesn't seem to have access to that either.

有没有一些方法获取内容长度或字节的托管管道发送?如果做不到这一点,是有一些方法我可以计算的内容长度,或从托管管道可用对象发送的字节?

Is there some way of getting the content-length or bytes sent in the managed pipeline? Failing that, is there some way I can calculate content-length or bytes sent from objects available in the managed pipeline?

推荐答案

要在得到您发送可以使用 HttpResponse.Filter字节 属性。正如MSDN文档说这个属性获取或设置用于传输之前修改HTTP实体主体的包装滤镜对象。

To get at the bytes sent you can use the HttpResponse.Filter property. As the MSDN docs say this property gets or sets a wrapping filter object that is used to modify the HTTP entity body before transmission.

您可以创建一个新的的System.IO.Stream 一个包装现有的 HttpResponse.Filter 流计算中传递的字节在传递它们之前写方法。例如:

You can create a new System.IO.Stream that wraps the existing HttpResponse.Filter stream and counts the bytes passed in to the Write method before passing them on. For example:

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

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

    void OnEndRequest(object sender, EventArgs e)
    {
        var application = (HttpApplication) sender;
        var contentLengthFilter = (ContentLengthFilter) application.Response.Filter;
        var contentLength = contentLengthFilter.BytesWritten;
    }

    public void Dispose()
    {
    }
}

public class ContentLengthFilter : Stream
{
    private readonly Stream _responseFilter;

    public int BytesWritten { get; set; }

    public ContentLengthFilter(Stream responseFilter)
    {
        _responseFilter = responseFilter;
    }

    public override void Flush()
    {
        _responseFilter.Flush();
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return _responseFilter.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        _responseFilter.SetLength(value);
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        return _responseFilter.Read(buffer, offset, count);
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        BytesWritten += count;
        _responseFilter.Write(buffer, offset, count);
    }

    public override bool CanRead
    {
        get { return _responseFilter.CanRead; }
    }

    public override bool CanSeek
    {
        get { return _responseFilter.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return _responseFilter.CanWrite; }
    }

    public override long Length
    {
        get { return _responseFilter.Length; }
    }

    public override long Position
    {
        get { return _responseFilter.Position; }
        set { _responseFilter.Position = value; }
    }
}

这篇关于IIS 7管理模块无法获取发送内容长度或字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆