如何从HttpModule中检索响应html?

时间:2009-09-04 17:28:46

标签: c# asp.net html httpmodule

以下是我特别想要做的事情:

我编写了一个HttpModule来进行一些特定于站点的跟踪。我们网站上的一些旧.aspx页面是硬编码的,没有真正的控件,但它们是.aspx文件,所以我的模块在请求时仍然运行。

我的模块的处理程序附加到PostRequestHandlerExecute,所以我相信应该已经确定将发送回请求者的内容。

我需要能够提取标题标签中的任何字符串。

所以,如果

<title>Chunky Bacon</title>

在最终呈现的HTML中发送给请求者。然后我想要“Chunky Bacon”。

想法?

2 个答案:

答案 0 :(得分:25)

有趣的小挑战。

以下是代码:

<强> StreamWatcher.cs

    public class StreamWatcher : Stream
    {
        private Stream _base;
        private MemoryStream _memoryStream = new MemoryStream();

        public StreamWatcher(Stream stream)
        {
            _base = stream;
        }

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

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

        public override void Write(byte[] buffer, int offset, int count)
        {
            _memoryStream.Write(buffer, offset, count);
            _base.Write(buffer, offset, count);
        }

        public override string ToString()
        {
            return Encoding.UTF8.GetString(_memoryStream.ToArray());
        }

        #region Rest of the overrides
        public override bool CanRead
        {
            get { throw new NotImplementedException(); }
        }

        public override bool CanSeek
        {
            get { throw new NotImplementedException(); }
        }

        public override bool CanWrite
        {
            get { throw new NotImplementedException(); }
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override long Length
        {
            get { throw new NotImplementedException(); }
        }

        public override long Position
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        #endregion
    }

<强> TitleModule.cs

public class TitleModule : IHttpModule
{
    public void Dispose()
    {
    }

    private static Regex regex = new Regex(@"(?<=<title>)[\w\s\r\n]*?(?=</title)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
    private StreamWatcher _watcher;
    public void Init(HttpApplication context)
    {
        context.BeginRequest += (o, e) => 
        {
            _watcher = new StreamWatcher(context.Response.Filter);
            context.Response.Filter = _watcher;
        };


        context.EndRequest += (o, e) =>
        {
            string value = _watcher.ToString();
            Trace.WriteLine(regex.Match(value).Value.Trim());
        };
    }
}

答案 1 :(得分:3)

有一篇关于4GuysFromRolla的文章讨论了创建HttpResponse过滤器的问题,这些过滤器基本上是在将响应传递给最终输出流(拦截器)之前处理响应的流。

http://aspnet.4guysfromrolla.com/articles/120308-1.aspx