【问题标题】:How can I grab the response text of web page via IIS Module?如何通过 IIS 模块获取网页的响应文本?
【发布时间】:2011-10-27 06:31:57
【问题描述】:

我正在开发一个 IIS 模块,当发出网页请求时,它会查看传递回浏览器的数据,并用批准的关键字替换某些关键字。我意识到有多种方法可以做到这一点,但就我们的目的而言,IIS 模块效果最好。

如何将发送回浏览器的数据流读取为字符串,以便根据需要转换关键字?

任何帮助将不胜感激!

代码如下:

namespace MyNamespace
{
    class MyModule : IHttpModule
    {
        private HttpContext _current = null;

        #region IHttpModule Members

        public void Dispose()
        {
            throw new Exception("Not implemented");
        }

        public void Init(HttpApplication context)
        {
            _current = context.Context;

            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
        }

        #endregion

        public void context_PreRequestHandlerExecute(Object source, EventArgs e)
        {
            HttpApplication app = (HttpApplication)source;
            HttpRequest request = app.Context.Request;
        }
}

【问题讨论】:

    标签: c# iis iis-modules


    【解决方案1】:

    有两种方式:

    1. 使用响应过滤器

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

    1. 处理应用程序的PreRequestHandlerExecute 事件,因为它在IHttpHandler 处理页面本身之前运行:

      public class NoIndexHttpModule : IHttpModule
      {
        public void Dispose() { }
      
        public void Init(HttpApplication context)
        {
          context.PreRequestHandlerExecute += AttachNoIndexMeta;
        }
      
        private void AttachNoIndexMeta(object sender, EventArgs e)
        {
          var page = HttpContext.Current.CurrentHandler as Page;
          if (page != null && page.Header != null)
          {
            page.Header.Controls.Add(new LiteralControl("<meta name=\"robots\" value=\"noindex, follow\" />"));
          }
        }
      

      }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多