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请求信息流进行层层的转交动作,直到返回到客户端为止。 (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如下:

        1.System.Web.SessionState.SessionStateModule;

        2.System.Web.Security.WindowsAuthenticationModule;

        3.System.Web.Security.FormsAuthenticationModule;

        4.System.Web.Security.PassportAuthenticationModule;

        5.System.Web.Security.UrlAuthorizationModule;

        6.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:
myHttpModule" type="ClassLibrary1.MyHttpModule,ClassLibrary1"/>

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("来自Default.aspx页面");
    }

}

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

        { }



        ///

        /// 验证HttpModule事件机制

        ///

        ///

        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
");

        }



        private void application_EndRequest(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_EndRequest
");

        }



        private void application_PreRequestHandlerExecute(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_PreRequestHandlerExecute
");

        }



        private void application_PostRequestHandlerExecute(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_PostRequestHandlerExecute
");

        }



        private void application_ReleaseRequestState(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_ReleaseRequestState
");

        }



        private void application_AcquireRequestState(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_AcquireRequestState
");

        }



        private void application_PreSendRequestContent(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_PreSendRequestContent
");

        }



        private void application_PreSendRequestHeaders(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_PreSendRequestHeaders
");

        }



        private void application_ResolveRequestCache(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_ResolveRequestCache
");

        }



        private void application_AuthorizeRequest(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_AuthorizeRequest
");

        }



        private void application_AuthenticateRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            application.Context.Response.Write("application_AuthenticateRequest");

        }

    }

}

HttpModule1" type="MyHttpModule.HttpModule1,MyHttpModule"/>

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事件。
												

asp.net的HTTP请求处理过程的更多相关文章

  1. 【转】各版本IIS下ASP.net请求处理过程区别

    原文地址:http://www.cnblogs.com/fsjohnhuang/articles/2332074.html ASP.NET是一个非常强大的构建Web应用的平台,它提供了极大的灵活性和能 ...

  2. ASP.NET请求处理过程

    当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给Aspnet_isapi.dll,aspnet_isapi.dll会通 ...

  3. 各版本IIS下ASP.net请求处理过程区别

      ASP.NET是一个非常强大的构建Web应用的平台,它提供了极大的灵活性和能力以致于可以用它来构建所有类型的Web应用. 绝大多数的人只熟悉高层的框架如: WebForms 和 WebServic ...

  4. IIS5/6/7 ASP.net 请求处理过程

    tks:http://www.cnblogs.com/TomXu/p/3756774.html 主要内容 本文讲解的是:服务器接受Http Request请求之后,是如何进入.Net CLR,从而进一 ...

  5. 基础知识系列☞各版本下IIS请求处理过程区别

    转载地址→http://www.cnblogs.com/fsjohnhuang/articles/2332074.html ASP.NET是一个非常强大的构建Web应用的平台, 它提供了极大的灵活性和 ...

  6. MVC的HTTP请求处理过程(IIS应用程序池、CLR线程池)

    主要内容 本文讲解的是:服务器接受Http Request请求之后,是如何进入.Net CLR,从而进一步操作的. 我们大家都知道,IIS必须先接受请求,然后才能有机会进入CLR,但对请求(reque ...

  7. Asp.Net构架(Http请求处理流程)、(Http Handler 介绍)、(HttpModule 介绍)

    Asp.Net构架(Http请求处理流程) Http请求处理流程概述 对于普通访问者来说,这就像每天太阳东边升起西边落下一样是理所当然的:对于很多程序员来说,认为这个与己无关,不过是系统管理员或者网管 ...

  8. http请求在asp.net中的请求过程

    当请求一个*.aspx文件的时候,这个请求会被inetinfo.exe进程截获,它判断文件的后缀(aspx)之后,将这个请求转交给 ASPNET_ISAPI.dll,ASPNET_ISAPI.dll会 ...

  9. http服务详解(1)——一次完整的http服务请求处理过程

    前言:要熟练掌握一个服务,首先需要非常了解这个服务的工作过程,这篇就详细解释了http服务的请求处理过程. 一次完整的http请求处理过程 (1)流程图 (2)过程详解 0.DNS域名解析:递归查询. ...

随机推荐

  1. 如何为 Go 设计一个通用的日志包

    需求 一个通用的日志包,应该满足以下几个需求: 兼容 log.Logger,标准库大量使用了 log.Logger 作为其错误内容的输出通道,比如 net/http.Server.ErrorLog,所 ...

  2. Java reflect 反射 1

    1 反射的概述 反射含义:可以获取正在运行的Java对象. JAVA反射机制是在运行状态中,对于任意一个类,都能够得到这个类的所有属性和方法; 对于任意一个对象,都能够调用它的任意一个方法; 这种动态 ...

  3. 使gitignore生效

    git rm -r --cached . // 删除本地缓存 git add . // 添加要提交的文件 初次提交直接声明gitignore并提交就可以: 非初次提交,改动的gitignore要进行上 ...

  4. MyBatis Generator 详解(转)

    MyBatis Generator中文文档 MyBatis Generator中文文档地址:http://mbg.cndocs.tk/ 该中文文档由于尽可能和原文内容一致,所以有些地方如果不熟悉,看中 ...

  5. Spring---AOP与DI的初步理解

    依赖注入 依赖注入并没有我们听上去那么复杂,在项目中应用依赖注入,会使代码变的异常简单,更易于理解和测试. 任何一个有实际意义的应用,都是多个类组成,这些类之间相互协作,来实现特定的业务逻辑,通常,每 ...

  6. 简单封装axios api

    可以在代码逻辑中写axios请求,处理请求结果,但是随着项目越来越大,代码会很繁琐,不容易维护,所以,可以把一些在所有请求中都要处理的逻辑抽取出来,封装成api方法.比如每次请求中都要判断是否有权限, ...

  7. 设置placeholder的字体颜色

    //设置字体颜色 [self.searchTextField setValue:[UIColor colorWithRed:0.50 green:0.50 blue:0.50 alpha:1.0] f ...

  8. 10、List、Set

    List接口 List接口的特点 *A:List接口的特点: a:它是一个元素存取有序的集合. 例如,存元素的顺序是11.22.33.那么集合中,元素的存储就是按照11.22.33的顺序完成的). b ...

  9. java多线程(一)-五种线程创建方式

    简单使用示例 Java 提供了三种创建线程的方法: 通过实现 Runnable 接口: 通过继承 Thread 类本身: 通过 Callable 和 Future 创建线程. 还有 定时器 线程池 下 ...

  10. Tempter of the Bone 搜索---奇偶性剪枝

    Tempter of the Bone Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) ...