博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于IHttpModule的相关知识总结
阅读量:7036 次
发布时间:2019-06-28

本文共 6472 字,大约阅读时间需要 21 分钟。

一、IHttpModule相关概述

相关IHttpModule接口:
using System;namespace System.Web{    public interface IHttpModule    {        //   销毁不再被HttpModule使用的资源        void Dispose();        // 初始化一个Module,为捕获HttpRequest做准备        void Init(HttpApplication context);    }}

 

功能概述:向实现类提供模块初始化和处置事件.它能够截获所有请求,如同windows hook一样.所以你能够利用它做很多你想要做的事情.

必然url重写、大文件上传、sql注入检测......

Asp.net会为每个请求分配一个HttpApplication对象.

管道

二、IIS7、IIS6 web.config注册HttpModule

1)IIS6注册HttpModule

    
      
    
  

2)IIS7注册HttpModule

<system.webServer>

<modules>

   <add name="..."    type="MyApplication.RequestDurationLoggerModule, MyApplication"/>

</modules>

</system.webServer>

三、动态注册

四、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发送数据。

五、完整的httpmodule运行过程

HttpModuleHandler

说明:

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

再次说明: 

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

验证生命周期代码:

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和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事件。看下面的图示:

 

出处:http://www.cnblogs.com/humble/p/3913078.html

你可能感兴趣的文章
关机命令
查看>>
禁止浏览器缓存的响应头和定时刷新
查看>>
Win8.1下安装Sass Compass
查看>>
sjtu oj 1250 最大连续子序列问题变形
查看>>
OSPF在企业网络中的应用
查看>>
运维核心之一 CMDB
查看>>
python高性能编程--002--全局解释器锁GIL
查看>>
VSS 信息安全
查看>>
python基础学习笔记
查看>>
Java的HashMap和HashTable
查看>>
我的友情链接
查看>>
windows系统之WSUS服务器:更改WSUS更新文件的路径
查看>>
Btrace
查看>>
我的友情链接
查看>>
python抓取豆瓣妹子图片并上传到七牛
查看>>
关于Spring Data redis几种对象序列化的比较
查看>>
windows下批处理设置U盘盘符为U【非PE】
查看>>
Windows系统补丁KB2962872导致InstallShield无法启动(解决方案已更新)
查看>>
#每天问自己个问题#0. 每天问自己个问题
查看>>
制作免费的数字签名证书
查看>>