IHttpHandler做文件防盗链接

IHttpHandler是ASP.NET处理实际操作的接口。在MSDN是这样定义的:使用自定义的HTTP处理程序同步处理HTTP Web请求而实现的协定。(注意:这里写的很清楚是同步HTTP请求如果是异步的话就要使用IHttpAsyncHandler接口程序了)。他包含一个属性IsReusable用于获取当前IHttpHandler实例是否可用一般设置为True.一个方法ProcessRequest(HttpContext context)进行实际的操作过程。

自定义IHttpHandler

使用自定义的IHttpHandler处理程序需要三步

1)定义一个实现了IHttpHandler接口的类。

2)在Web.config文件中注册这个类

3)执行相应的HttpRequst

下面是一个图片防盗链接的实例:

01 public class LinkProtectHandler: IHttpHandler
02     {
03         #region IHttpHandler 成员
04   
05         public bool IsReusable
06         {
07             get { return true; }
08         }
09   
10         public void ProcessRequest(HttpContext context)
11         {
12             //具体执行的操作
13             //获取文件服务器端物理路径
14             string fileName = context.Server.MapPath(context.Request.FilePath);
15             //如果UrlReferrer为空则显示一张默认的禁止盗链的图片
16             if (context.Request.UrlReferrer.Host==null)
17             {
18                 context.Response.ContentType = "image/JPEG";
19                 context.Response.WriteFile("/LinkProtect.jpg");
20             }
21             else
22             {
23                 //如果UrlReferrer中不包含自己站点的主机域名,则显示一张默认的禁止盗链图片
24                 if (context.Request.UrlReferrer.Host.IndexOf("mydomain.com")>0)
25                 {
26                     context.Response.ContentType = "image/JPEG";
27                     context.Response.WriteFile(fileName);
28                 }
29                 else
30                 {
31                     context.Response.ContentType = "image/JPEG";
32                     context.Response.WriteFile("/LinkProtect.jpg");
33                 }
34             }
35         }
36   
37         #endregion
38     }
1 Web.Config里面注册如下
1 <httpHandlers>
2   <remove verb="*" path="*.asmx"/>
3   <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
4   <add verb="*" path="*.jpeg" validate="false" type="WebApplication3.LinkProtectHandler,WebApplication3"/>
5 </httpHandlers>
自定义IHttpHandlerFactory

IHttpHandlerFactory在MSDN中是这样定义的:定义类工厂为创建新的IHttpHandler对象而必须实现的协定。他包含2个接口方法,GetHandler():返回实现IHttp接口的类的实例。ReleaseHandler():始工厂可以重复使用现有的处理程序实例。这个接口就是可以把很多实现IHttpHandler接口的方法放在Factory中在Web.config注册时主要注册这个类即可,在使用的时候交由Factory进行判断到底使用哪个IHttpHandler实例。

01 public class CustomerHttpHandlerFactory:IHttpHandlerFactory
02 {
03  
04     #region IHttpHandlerFactory 成员
05  
06     public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
07     {
08         //获取文件服务器物理路径
09         string path = context.Request.PhysicalPath;
10         //获取文件名后缀名
11         string exstention = Path.GetExtension(path);
12         //进行判断,交由相应的处理程序
13         if (exstention==".rss")
14         {
15             return new RssHandler();
16         }
17         else if (exstention==".atmo")
18         {
19             return new ATMOHandler();
20         }
21         else
22         {
23             return null;
24         }
25     }
26  
27     public void ReleaseHandler(IHttpHandler handler)
28     {
29           
30     }
31  
32     #endregion
33 }

在Web.Config里面进行如下定义:

1 <httpHandlers>
2   <remove verb="*" path="*.asmx"/>
3   <add verb="*" path="*.rss,*.atom" validate="false" type="WebApplication3.CustomerHttpHandlerFactory,WebApplication3"/>
4 </httpHandlers>
IHttpAsyncHandler异步接口

IHttpAsyncHandler在MSDN中是这样描述的:定义 HTTP 异步处理程序对象必须实现的协定。他包含3个方法:

BeginProcessRequest:启动对 HTTP 处理程序的异步调用,EndProcessRequest:进程结束时提供异步处理 End 方法。ProcessRequest:通过实现 IHttpHandler 接口的自定义 HttpHandler 启用 HTTP Web 请求的处理。 (继承自 IHttpHandler。)和一个IsReusable属性。

原文地址:https://www.cnblogs.com/dfsxh/p/1937458.html