Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过IIS模块获取网页的响应文本?_C#_Iis_Iis Modules - Fatal编程技术网

C# 如何通过IIS模块获取网页的响应文本?

C# 如何通过IIS模块获取网页的响应文本?,c#,iis,iis-modules,C#,Iis,Iis Modules,我正在开发一个IIS模块,当发出网页请求时,它会查看传递回浏览器的数据,并用批准的关键字替换某些关键字。我意识到有多种方法可以做到这一点,但就我们的目的而言,IIS模块将工作得最好 如何将发送回浏览器的数据流读入字符串,以便根据需要转换关键字 任何帮助都将不胜感激 代码如下: namespace MyNamespace { class MyModule : IHttpModule { private HttpContext _current = null;

我正在开发一个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;
        }
}
有两种方法:

  • 使用响应过滤器
  • 处理应用程序的
    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\" />"));
        }
      }
    
    public类NoIndexHttpModule:IHttpModule
    {
    public void Dispose(){}
    公共void Init(HttpApplication上下文)
    {
    context.PreRequestHandlerExecute+=AttachNoIndexMeta;
    }
    私有void AttachNoIndexMeta(对象发送方,事件参数e)
    {
    var page=HttpContext.Current.CurrentHandler作为页面;
    if(page!=null&&page.Header!=null)
    {
    page.Header.Controls.Add(新的LiteralControl(“”);
    }
    }
    
    }