如何从 HttpModule 中检索响应 html?

发布于 2024-08-04 08:10:56 字数 354 浏览 7 评论 0原文

这是我特别想做的事情:

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

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

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

因此,if

<title>Chunky Bacon</title>

在最终渲染的 HTML 中发送给请求者。然后我想要“厚块培根”。

有想法吗?

Here is what I'm specifically trying to do:

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.

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.

So if

<title>Chunky Bacon</title>

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

Ideas?

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

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

发布评论

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

评论(2

难忘№最初的完美 2024-08-11 08:10:56

有趣的小挑战。

代码如下:

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

Fun little challenge.

Here's the 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());
        };
    }
}
有木有妳兜一样 2024-08-11 08:10:56

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

https://web. archive.org/web/20210510022033/https://aspnet.4guysfromrolla.com/articles/120308-1.aspx

There is an article on 4GuysFromRolla that talks about creating HttpResponse filters which are basically streams that process the response before passing it through to the final output stream (an interceptor).

https://web.archive.org/web/20210510022033/https://aspnet.4guysfromrolla.com/articles/120308-1.aspx

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