1、asp.net的HTTP请求处理过程

说明: 
(1)、客户端浏览器向服务器发出一个http请求,此请求会被inetinfo.exe进程截获,然后转交给aspnet_isapi.dll进程,接着它又通过Http Pipeline的管道,传送给aspnet_wp.exe这个进程,接下来就到了.net framework的HttpRunTime处理中心,处理完毕后就发送给用户浏览器。 
(2)、当一个http请求被送入到HttpRuntime之后,这个Http请求会继续被送入到一个被称之为HttpApplication Factory的一个容器当中,而这个容器会给出一个HttpApplication实例来处理传递进来的http请求,而后这个Http请求会依次进入到如下几个容器中:HttpModule --> HttpHandler Factory --> HttpHandler。当系统内部的HttpHandler的ProcessRequest方法处理完毕之后,整个Http Request就被处理完成了,客户端也就得到相应的东东了。 
(3)完整的http请求在asp.net framework中的处理流程: 
HttpRequest-->inetinfo.exe->ASPNET_ISAPI.DLL-->Http Pipeline-->ASPNET_WP.EXE-->HttpRuntime-->HttpApplication Factory-->HttpApplication-->HttpModule-->HttpHandler Factory-->HttpHandler-->HttpHandler.ProcessRequest()

也就是说一个HTTP请求在HttpModule容器的传递过程中,会在某一时刻(ResolveRequestCache事件)将这个HTTP请求传递给HttpHandler容器。在这个事件之后,HttpModule容器会建立一个HttpHandler的入口实例,但是此时并没有将HTTP请求控制权交出,而是继续触发AcquireRequestState事件以及PreRequestHandlerExcute事件。在PreRequestHandlerExcute事件之后,HttpModule窗口就会将控制权暂时交给HttpHandler容器,以便进行真正的HTTP请求处理工作。

而在HttpHandler容器内部会执行ProcessRequest方法来处理HTTP请求。在容器HttpHandler处理完毕整个HTTP请求之后,会将控制权交还给HttpModule,HttpModule则会继续对处理完毕的HTTP请求信息流进行层层的转交动作,直到返回到客户端为止。 
PS:红色的HttpApplication实例在HttpModule的Init方法中会用到。

(4)如果想在中途截获一个httpRequest并做些自己的处理,就应该在HttpRuntime运行时内部来做到这一点,确切的说是在HttpModule这个容器中来实现。

2、HttpModule工作原理

负责监听HttpRequest,同时对HttpRequest增添或者过滤掉一部分内容。也就是说,当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于HTTP请求来讲,HttpModule是一个HTTP请求的“必经之路”,所以可以在这个HTTP请求传递到真正的请求处理中心(HttpHandler)之前附加一些需要的信息在这个HTTP请求信息之上,或者针对截获的这个HTTP请求信息作一些额外的工作,或者在某些情况下干脆终止满足一些条件的HTTP请求,从而可以起到一个Filter过滤器的作用。 
HttpModule实现了接口IHttpModule,我们可以自定义实现该接口的类,从而取代HttpModule。 
asp.net默认的HttpModule如下:

        System.Web.SessionState.SessionStateModule;
        System.Web.Security.WindowsAuthenticationModule;
        System.Web.Security.FormsAuthenticationModule;
        System.Web.Security.PassportAuthenticationModule;
        System.Web.Security.UrlAuthorizationModule;
        System.Web.Security.FileAuthorizationModule;

3、编写自己的HttpModule

要实现HttpModule,必须实现接口IHttpModule。下面是IHttpModule接口分析:

using System;

namespace System.Web
{
    public interface IHttpModule
    {
        //   销毁不再被HttpModule使用的资源
        void Dispose();

        // 初始化一个Module,为捕获HttpRequest做准备
        void Init(HttpApplication context);
    }
}

下面是自己的HttpModule:

using System;
using System.Web;

namespace ClassLibrary1
{
    public class MyHttpModule : IHttpModule
    {
        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(Application_BeginRequest);
            context.EndRequest += new EventHandler(Application_EndRequest);
        }

        public void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;
            HttpResponse response = context.Response;
            response.Write("这是来自自定义HttpModule中有BeginRequest");
        }

        public void Application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;
            HttpResponse response = context.Response;
            response.Write("这是来自自定义HttpModule中有EndRequest");
        }

    }
}

web.config

    <httpModules>
      <add name="myHttpModule" type="ClassLibrary1.MyHttpModule,ClassLibrary1"/>
    </httpModules>

default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("<br/><br/>来自Default.aspx页面<br/>");
    }
}

4、HttpModule内部事件机制和生命周期

HttpModule对HttpApplication实例进行处理,而HttpApplication有很多事件(对应不同的生命期),这样就衍生出HttpModule内部事件机制和生命周期。 
(1)、HttpModule的事件

   
BeginRequest 指示请求处理开始
AuthenticateRequest 封装请求身份验证过程
AuthorizeRequest 封装检查是否能利用以前缓存的输出页面处理请求的过程
ResolveRequestCache 从缓存中得到相应时候触发
AcquireRequestState 加载初始化Session时候触发
PreRequestHandlerExecute 在Http请求进入HttpHandler之前触发
PostRequestHandlerExecute 在Http请求进入HttpHandler之后触发
ReleaseRequestState 存储Session状态时候触发
UpdateRequestCache 更新缓存信息时触发
EndRequest 在Http请求处理完成的时候触发
PreSendRequestHenaders 在向客户端发送Header之前触发
PreSendRequestConternt 在向客户端发送内容之前触发

说明: 
a、BenginRequest和EndRequest分别是HttpModule容器最开始的和最后的事件; 
b、EndRequest之后还会触发PreSendRequestHeaders事件和PreSendRequestContent事件,这不是在HttpModule外的两个事件,表示HttpModule结束,即将开始向Client发送数据。

(2)、验证HttpModule生命周期 
与HttpHandler的交互: 

说明: 
a、HttpModule容器会将HttpRequest传递到HttpHandler容器,这个时间点是ResolveRequestCache事件 
b、HttpModule容器会建立HttpHandler实例作为入口——Session从此生效 
c、触发AcquireRequestState事件以及PreRequestHandlerExecute事件 
d、HttpModule容器便将对HttpRequest的控制权转让给HttpHandler容器 
e、HttpHandler容器处理HttpRequest——使用自身的ProcessRequest方法,将对其控制权又还给HttpModule容器——之后Session失效。 

验证生命周期代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace MyHttpModule
{
    public class ValidaterHttpModuleEvents : IHttpModule
    {

        public void Dispose()
        { }

        /// <summary>
        /// 验证HttpModule事件机制
        /// </summary>
        /// <param name="application"></param>
        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(application_BeginRequest);
            application.EndRequest += new EventHandler(application_EndRequest);
            application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
            application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);
            application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
            application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
            application.PostRequestHandlerExecute += new EventHandler(application_PostRequestHandlerExecute);
            application.ReleaseRequestState += new EventHandler(application_ReleaseRequestState);
            application.ResolveRequestCache += new EventHandler(application_ResolveRequestCache);
            application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);
            application.PreSendRequestContent += new EventHandler(application_PreSendRequestContent);
        }

        private void application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_BeginRequest<br/>");
        }

        private void application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_EndRequest<br/>");
        }

        private void application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_PreRequestHandlerExecute<br/>");
        }

        private void application_PostRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_PostRequestHandlerExecute<br/>");
        }

        private void application_ReleaseRequestState(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_ReleaseRequestState<br/>");
        }

        private void application_AcquireRequestState(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_AcquireRequestState<br/>");
        }

        private void application_PreSendRequestContent(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_PreSendRequestContent<br/>");
        }

        private void application_PreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_PreSendRequestHeaders<br/>");
        }

        private void application_ResolveRequestCache(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_ResolveRequestCache<br/>");
        }

        private void application_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_AuthorizeRequest<br/>");
        }

        private void application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_AuthenticateRequest<br/>");
        }
    }
}
<add name="HttpModule1" type="MyHttpModule.HttpModule1,MyHttpModule"/>
<add name="HttpModule2" type="MyHttpModule.HttpModule2,MyHttpModule"/>

HttpModule1和HttpModule2模仿ValidaterHttpModuleEvents编写(除了类名改变外,事件和方法不变),不贴代码了。运行结果如下:

从运行结果可以看到,在web.config文件中引入自定义HttpModule的顺序就决定了多个自定义HttpModule在处理一个HTTP请求的接管顺序。

(3)、利用HttpModule实现终止此次HttpRequest请求

在BeginRequest事件中,使用HttpApplication.CompleteRequest()方法可以实现当满足一定条件时终止此次HttpRequest请求

using System;
using System.Web;

namespace ClassLibrary1
{
    public class MyHttpModule : IHttpModule
    {
        public void Dispose() { }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(Application_BeginRequest);
        }

        public void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            application.CompleteRequest();
            application.Context.Response.Write("请求被终止");
        }
    }
}

说明: 
a、对于一个HttpModule,在BeginRquest中终止,但是仍然会调用EndRequest事件,以及PreSendRequestHeaders事件和PreSendRequestContent事件。也可以说是直接跳转到EndRequest事件,而不会调用这期间的事件 
b、如果有两个HttpModule,在第一个HttpModule的BeginRequest中终止,仅仅不会调用第二个HttpModule的BeginRequest,但仍然会调用两个EndRequest事件,以及PreSendRequestHeaders事件和PreSendRequestContent事件。看下面的图示:

HttpModule的认识的更多相关文章

  1. ASP.NET使用HttpModule压缩并删除空白Html请求

    当我们压缩我的Response后再传到Client端时,可以明显节省宽带. 提升Site的性能. 现在的浏览器大部分都支持Gzip,Deflate压缩. 同时我们还可以删除一些空白段,空行,注释等以使 ...

  2. [转]HttpModule的认识

    HttpModule是向实现类提供模块初始化和处置事件.当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于H ...

  3. [转]ASP.NET应用程序生命周期趣谈(三) HttpModule

    在之前的文章中,我们提到过P_Module(HttpModule)这个能干的程序员哥们儿,它通过在项目经理HttpApplication那里得到的授权,插手整个应用程序级别的事件处理.所有的HttpM ...

  4. 基于HttpModule的简单.NET网站授权方案

    摘要 本文介绍一种入门级的网站授权(注:这里所指的授权指的是注册码效果,而不是网站登陆时的身份授权)方案,仅供学习交流及对付小白客户使用.复杂的网站授权涉及网站加密等一系列复杂的技术,不做本文介绍内容 ...

  5. httphandler和httpmodule的区别

    ASP.Net处理Http Request时,使用Pipeline(管道)方式,由各个HttpModule对请求进行处理,然后到达 HttpHandler,HttpHandler处理完之后,仍经过Pi ...

  6. 你必须知道ASP.NET知识------关于动态注册httpmodule(对不起汤姆大叔)

    一.关于动态注册的问题 很多人看过汤姆大叔的MVC之前的那点事儿系列(6):动态注册HttpModule ,其实汤姆大叔没有发现httpmodule动态注册的根本机制在哪里. 亦即:怎么动态注册?为什 ...

  7. Asp.Net通过HttpModule实现URL重写

    首先总结一下为什么要对URL进行Rewrite,比如我可以把/Default.aspx?param=3替换成/Home/Default/3(类似mvc). 一.缩短url,隐藏实际路径提高安全性; 二 ...

  8. Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

    Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, ...

  9. 部署网站出现System.ServiceModel.Activation.HttpModule错误

    1. 部署网站到IIS7.5,Window 2008的时候出现这个错误 2. 错误信息 Server Error in '/' Application. Could not load type 'Sy ...

随机推荐

  1. js中转移符

    "<a href='javascript:;' onclick='javascript:changeChannelRuleStatus(\"" + options. ...

  2. TypeError: window.open is not a function

    想必大家现在都已经到家了,而苦逼的我还要坐在办公室混拿微薄的工资,技不如人,平常不努力给自己充电,年终一毛钱都没多给.不说这扫兴的话题了,在这给同样在苦逼坚守岗位的同志们节日的问候,新的一年,好运连连 ...

  3. html生成图片并保存到本地方法(Windows)

    // 最近用到一个保存html为图片到本地的功能(保存到下载目录),记之,该功能IE使用Blob 存储数据,关于兼容性问题参见如下表格,其他浏览器使用a标签download属性新功能下载 Browse ...

  4. cacti web页面访问 settings出错

    查看apache错误日志: 错误信息Mon Dec 26 11:00:48.241653 2016] [:error] [pid 32607] [client 192.168.10.79:65009] ...

  5. AseBulkCopy 若干问题的解决方法

    场景:数据库 Ase 15.0, Ado.net客户端15.7  Sybase.AdoNet4.AseClient 错误1:使用Transaction报错 错误2: 存储过程"sp_drv_ ...

  6. 2018. The Debut Album

    http://acm.timus.ru/problem.aspx?space=1&num=2018 真心爱过,怎么能彻底忘掉 题目大意: 长度为n的串,由1和2组成,连续的1不能超过a个,连续 ...

  7. Bootstrap<基础九>辅助类

    Bootstrap 中的一些可能会派上用场的辅助类. 文本 以下不同的类展示了不同的文本颜色.如果文本是个链接鼠标移动到文本上会变暗: 类 描述   .text-muted "text-mu ...

  8. Visual Basic 2012 借助DataGridView控件将Excel 2010数据导入到SQL server 2012

    (注:注释的颜色原本为绿色,在这里变为黑色,有点不便,但不会造成阅读影响.放入Visual Basic2012代码编辑器后会还原成绿色.) 摘  要:DataGridView控件作为数据传输的中介,只 ...

  9. 【墙内备份】Android 6.0 APIs

    Android 6.0 APIs In this documentSHOW MORE Fingerprint Authentication Confirm Credential App Linking ...

  10. 【转】Java反射 之 反射基础

    一.反射 反射:Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为 ...