PostRequestHandlerExecute 中的响应长度

发布于 2024-09-07 03:05:43 字数 104 浏览 2 评论 0原文

我想知道出于记录目的,事后发送给用户的响应到底有多长。有什么方法可以从 asp.net 中的 HttpModule 执行此操作(在 PostRequestHandlerExecute 事件中)。

I want to find out exactly how long the Response sent to the user was, after the fact, for logging purposes. Is there any way to do this from an HttpModule in asp.net (in the PostRequestHandlerExecute event).

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

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

发布评论

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

评论(2

独行侠 2024-09-14 03:05:43

不幸的是,HttpResponse.OutputStream只写,因此这不是很简单 - 任何尝试查看输出流的 Length 属性会抛出异常。

我见过的唯一解决方案是对 Response 对象应用过滤器,以便过滤器可以计算字节数。

快速 Google 搜索找到了我这里,这似乎接近实现我记得。

Unfortunately, HttpResponse.OutputStream is write-only, so this is not very straightforward - any attempts to look at the Length property of the output stream will throw an exception.

The only solution to this I've ever seen is by applying a filter to the Response object, so that the filter can count the bytes.

A quick Google search landed me here, which seems close to the implementation I remember.

佞臣 2024-09-14 03:05:43

希望它有帮助

context.PostRequestHandlerExecute += delegate(object sender, EventArgs e)
{
    HttpContext httpContext = ((HttpApplication)sender).Context;
    HttpResponse response = httpContext.Response;
    // Don't interfere with non-HTML responses
    if (response.ContentType == "text/html")
    {
        response.Filter = new MyRewriterStream(response.Filter);
    }
};

MyRewriterStream 类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace ExecutionTime
{
    public class MyRewriterStream:Stream
    {
        #region "Propiedades"

        private Stream _sink;

        #endregion
        public MyRewriterStream(System.IO.Stream stream)
        {

            _sink = stream;
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            string outStr;
            outStr = UTF8Encoding.UTF8.GetString(buffer, offset, count);
            strPageSize = strPageSize + outStr;
            StringBuilder sb = new StringBuilder(outStr);

            if (sb.ToString().LastIndexOf("</html>") > 0)
            {
                 string HtmlResponse = "";//HERE PUT YOUR NEW HTML RESPONSE
                 sb.AppendLine(HtmlResponse );
                 byteArray = Encoding.ASCII.GetBytes(sb.ToString());
                 _sink.Write(byteArray, 0, byteArray.Length);
            }
            else
            {
                _sink.Write(buffer, offset, count);
            }

        }

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


        #region Properites

        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        //public override void Flush()
        //{
        //    _sink.Flush();
        //}

        public override long Length
        {
            get { return 0; }
        }

        private long _position;
        public override long Position
        {
            get { return _position; }
            set { _position = value; }
        }

        #endregion

        #region Methods

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

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

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

        public override void Close()
        {
            _sink.Close();
        }
        #endregion
    }
}

Wish it Help

context.PostRequestHandlerExecute += delegate(object sender, EventArgs e)
{
    HttpContext httpContext = ((HttpApplication)sender).Context;
    HttpResponse response = httpContext.Response;
    // Don't interfere with non-HTML responses
    if (response.ContentType == "text/html")
    {
        response.Filter = new MyRewriterStream(response.Filter);
    }
};

MyRewriterStream Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

namespace ExecutionTime
{
    public class MyRewriterStream:Stream
    {
        #region "Propiedades"

        private Stream _sink;

        #endregion
        public MyRewriterStream(System.IO.Stream stream)
        {

            _sink = stream;
        }
        public override void Write(byte[] buffer, int offset, int count)
        {
            string outStr;
            outStr = UTF8Encoding.UTF8.GetString(buffer, offset, count);
            strPageSize = strPageSize + outStr;
            StringBuilder sb = new StringBuilder(outStr);

            if (sb.ToString().LastIndexOf("</html>") > 0)
            {
                 string HtmlResponse = "";//HERE PUT YOUR NEW HTML RESPONSE
                 sb.AppendLine(HtmlResponse );
                 byteArray = Encoding.ASCII.GetBytes(sb.ToString());
                 _sink.Write(byteArray, 0, byteArray.Length);
            }
            else
            {
                _sink.Write(buffer, offset, count);
            }

        }

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


        #region Properites

        public override bool CanRead
        {
            get { return true; }
        }

        public override bool CanSeek
        {
            get { return true; }
        }

        public override bool CanWrite
        {
            get { return true; }
        }

        //public override void Flush()
        //{
        //    _sink.Flush();
        //}

        public override long Length
        {
            get { return 0; }
        }

        private long _position;
        public override long Position
        {
            get { return _position; }
            set { _position = value; }
        }

        #endregion

        #region Methods

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

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

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

        public override void Close()
        {
            _sink.Close();
        }
        #endregion
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文