如何从一个HTTP模块内检索响应HTML? [英] How do I retrieve response html from within a HttpModule?

查看:118
本文介绍了如何从一个HTTP模块内检索响应HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面就是我特别想要做的事:

Here is what I'm specifically trying to do:

我已经写了的HttpModule做一些具体的现场跟踪。我们网站上的一些旧的.aspx页面是很难codeD,没有真正的控制,但他们是.aspx文件,所以我的模块,在需要的时候,他们仍然运行。

I have written a HttpModule to do some site specific tracking. Some old .aspx pages on our site are hard coded with no real controls, but they are .aspx files so my module still runs when they are requested.

我的模块的处理程序连接到PostRequestHandlerExecute,所以我相信什么都会被送回应该已经确定请求者。

My module's handler is attached to the PostRequestHandlerExecute, so I believe what will be sent back to the requester should have already been determined.

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

I need to be able to extract whatever string is in the title tag.

所以,如果

<title>Chunky Bacon</title>

在最终呈现的HTML发送给请求者。然后我想矮胖培根。

is sent to the requester in the final rendered HTML. Then I want "Chunky Bacon".

想法?

推荐答案

有趣的小的挑战。

这里的code:

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());
        };
    }
}

这篇关于如何从一个HTTP模块内检索响应HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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