惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 噢耶游戏

星际迷航 致、青春 12306火车票网站自动登录工具 - 噢耶游戏 Compile your AS3 projects with Ant + Flex SDK (all Open Source) 有史以来关于Flash Player的最详细说明 深入理解Flash Player的应用程序域(Application Domains)(转载) - 噢耶游戏 弄了个新blogs 欢迎捧场 - 噢耶游戏 PHP 实用便捷代码(转) 一个鼠标拖出一个圆形的简单demo Flex中不使用FelxPrintJob通过ExternalInterface直接调用JavaScript利用浏览器本身的功能进行打印 Flex 拖拽 - 噢耶游戏 - 博客园 ASP.NET防止盗链(转) CS0016: 未能写入输出文件“C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\webapp\2adfa36a\1a90a869\8_prkz0n.dll" 错误的解决办法。 - 噢耶游戏 FLEX 12个基础 很有用的方法 取得工作目录下的文件 Asp.net中防止用户多次登录的方法(转) 大型网站的系统架构(摘) ActionSrcipt 资源 - 噢耶游戏 Flex和.net Webservice之间多层结构开发
HttpModule 工作原理(转)
噢耶游戏 · 2008-07-30 · via 博客园 - 噢耶游戏

当一个HTTP请求到达HttpModule时,整个ASP.NET Framework系统还并没有对这个HTTP请求做任何处理,也就是说此时对于HTTP请求来讲,HttpModule是一个HTTP请求的“必经之路”,所以可以在这个HTTP请求传递到真正的请求处理中心(HttpHandler)之前附加一些需要的信息在这个HTTP请求信息之上,或者针对截获的这个HTTP请求信息作一些额外的工作,或者在某些情况下干脆终止满足一些条件的HTTP请求,从而可以起到一个Filter过滤器的作用。

示例1

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

namespace MyHttpModule

{

     /// <summary>

     /// 说明:用来实现自己的HttpModule类。

     /// 作者:文野

     /// 联系:stwyhm@cnblogs.com

     /// </summary>

     public class MyFirstHttpModule : IHttpModule

     {

         private void Application_BeginRequest(object sender, EventArgs e)

         {

              HttpApplication application = (HttpApplication)sender;

              HttpContext context = application.Context;

              HttpRequest request = application.Request;

              HttpResponse response = application.Response;

              response.Write("我来自自定义HttpModule中的BeginRequest<br />");

         }

         private void Application_EndRequest(object sender, EventArgs e)

         {

              HttpApplication application = (HttpApplication)sender;

              HttpContext context = application.Context;

              HttpRequest request = application.Request;

              HttpResponse response = application.Response;

              response.Write("我来自自定义HttpModule中的EndRequest<br />");

         }

         #region IHttpModule 成员

         public void Dispose()

         {}

         public void Init(HttpApplication application)

         {

              application.BeginRequest += new EventHandler(Application_BeginRequest);

              application.EndRequest += new EventHandler(Application_EndRequest);

         }

         #endregion

     }

}

Web.config进行如下配置

<add name="MyFirstHttpModule" type="MyHttpModule.MyFirstHttpModule,MyHttpModule"/>


 


深入了解
HttpModule

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

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


1HttpModule生命周期示意图

示例2:验证HttpModule生命周期

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

namespace MyHttpModule

{

    public class ValidaterHttpModule : IHttpModule

    {

        #region IHttpModule 成员

        public void Dispose()

        {}

        public void Init(HttpApplication application)

        {

            application.BeginRequest += new EventHandler(application_BeginRequest);

            application.EndRequest += new EventHandler(application_EndRequest);

            application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);

            application.PostRequestHandlerExecute += new EventHandler(application_PostRequestHandlerExecute);

            application.ReleaseRequestState += new EventHandler(application_ReleaseRequestState);

            application.AcquireRequestState += new EventHandler(application_AcquireRequestState);

            application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);

            application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);

            application.ResolveRequestCache += new EventHandler(application_ResolveRequestCache);

            application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);

            application.PreSendRequestContent += new EventHandler(application_PreSendRequestContent);

        }

        void application_PreSendRequestContent(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_PreSendRequestContent<br/>");

        }

        void application_PreSendRequestHeaders(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_PreSendRequestHeaders<br/>");

        }

        void application_ResolveRequestCache(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_ResolveRequestCache<br/>");

        }

        void application_AuthorizeRequest(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_AuthorizeRequest<br/>");

        }

        void application_AuthenticateRequest(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_AuthenticateRequest<br/>");

        }

        void application_AcquireRequestState(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_AcquireRequestState<br/>");

        }

        void application_ReleaseRequestState(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_ReleaseRequestState<br/>");

        }

        void application_PostRequestHandlerExecute(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_PostRequestHandlerExecute<br/>");

        }

        void application_PreRequestHandlerExecute(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_PreRequestHandlerExecute<br/>");

        }

        void application_EndRequest(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_EndRequest<br/>");

        }

        void application_BeginRequest(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.Context.Response.Write("application_BeginRequest<br/>");

        }

        #endregion

    }

}


 

多个自定义的Http Module的运作

从运行结果可以看到,在web.config文件中引入自定义HttpModule的顺序就决定了多个自定义HttpModule在处理一个HTTP请求的接管顺序。注:系统默认那几个HttpModule是最先衩ASP.NET Framework所加载上去的。

示例3:(代码类同示例2


 


HttpModule中终止此次的HTTP请求

可以利用HttpModule通过调用HttpApplication.CompleteRequest()方法实现当满足某一个条件时终止此次的HTTP请求。

需要注意的是,即使调用了HttpApplication.CompleteRequest()方法终止了一个HTTP请求,ASP.NET Framework仍然会触发HttpApplication后面的这3个事件:EndRequest事件、PreSendRequestHeaders事件、PreSendRequestContent事件。

如果存在多个自定义的HttpModule的话,当Module1终止了一个HTTP请求,这个HTTP请求将不会再触发Module2中相应的事件了,但Module2的最后三个事件仍会被触发。

示例4

using System;

using System.Collections.Generic;

using System.Text;

using System.Web;

namespace MyHttpModule

{

    public class CompleteRequestHttpModule : IHttpModule

    {

        #region IHttpModule 成员

        public void Dispose()

        {}

        public void Init(HttpApplication application)

        {

            application.BeginRequest += new EventHandler(Application_BeginRequest);

        }

        void Application_BeginRequest(object sender, EventArgs e)

        {

            HttpApplication application = (HttpApplication)sender;

            application.CompleteRequest();

            application.Context.Response.Write("请求被终止。");

        }

        #endregion

    }

}


 

Tags: httpmodule

噢耶游戏是中国最大的轻社交游戏开发商,致力于手机页游的研发及推广业务。我们首创性地提出了HTML5游戏中心思路,在第三方App 中嵌入式休闲游戏,为开发者提供了全新的应用内游戏解决方案。