HttpHandler

HttpHandler初探

一、HttpHandler简介

      HttpHandler是asp.net真正处理Http请求的地方。在这个HttpHandler容器中,ASP.NET Framework才真正地对客户端请求的服务器页面做出编译和执行,并将处理过后的信息附加在HTTP请求信息流中再次返回到HttpModule中。

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

     HttpHandlerHttpModule不同,一旦定义了自己的HttpHandler类,那么它对系统的HttpHandler的关系将是“覆盖”关系。

二、IHttpHandler的处理过程

      当一个HTTP请求经过HttpModule容器传递到HttpHandler容器中时,ASP.NET Framework会调用HttpHandler的ProcessRequest成员方法来对这个HTTP请求进行真正的处理。以一个ASPX页面为例,正是在这里一个ASPX页面才被系统处理解析,并将处理完成的结果继续经由HttpModule传递下去,直至到达客户端。

对于ASPX页面,ASP.NET Framework在默认情况下是交给System.Web.UI.PageHandlerFactory这个HttpHandlerFactory来处理的。所谓一个HttpHandlerFactory,是指当一个HTTP请求到达这个HttpHandlerFactory时,HttpHandlerFactory会提供出一个HttpHandler容器,交由这个HttpHandler容器来处理这个HTTP请求。

一个HTTP请求都是最终交给一个HttpHandler容器中的ProcessRequest方法来处理的。

      HttpHandler

三、一个简单的自定义IHttpHandler

      实现一个自定义的IHttpHandler需要两步:

      第一:定义一个实现了IHttpHandler的类,并且实现其ProcessRequest方法。在一个HttpHandler容器中如果需要访问Session,必须实现IRequiresSessionState接口,这只是一个标记接口,没有任何方法。       

HttpHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web;
namespace MyHttpHandler {     public class HelloHttpHandler:IHttpHandler     {         public bool IsReusable          {             get { return true; }         }
        public void ProcessRequest(HttpContext context)          {             context.Response.Write("我是经过HelloHttpHandler处理的!");         }     } }
HttpHandler

      第二:在web.config中注册这个类,并且指定Handler处理的请求类型

               把此节点插入system.web节点中

  <httpHandlers>       <!--path中指定的是执行type中HttpHandler的访问路径。此路径可以带后缀也可以不带后缀。如果path配置为*,则会对所有的请求执行此HttpHandler-->       <add  verb="*" path="httphandlerlearn.html" type="MyHttpHandler.HelloHttpHandler,MyHttpHandler"/>     </httpHandlers>

       这时,访问httphandlerlearn.html就会显示“我是经过HelloHttpHandler处理的!”,如下:

       HttpHandler

五、HttpHandlerFactory

      ASP.NET Framework实际不直接将相关页面的HTTP请求定位到一个其内部默认的IHttpHandler容器之上,而定位到了其内部默认的IHttpHandlerFactory上。IHttpHandlerFactory的作用是对IHttpHandler容器进行调度和管理。

    如果我们自定义的HttpHandler比较多,会在Web.config中注册很多HttpHandler类,这时把这些HttpHandler通过一个HttpHandlerFactory来集中管理就显的非常必要,此时只需要在Web.config中注册此HttphandlerFactory即可。

IHttpHandlerFactory接口包含两个方法。GetHandler返回实现IHttpHandler接口的类的实例,ReleaseHandler使工厂可以重用现有的处理程序实例。

      实现一个自定义的HttphandlerFactory同样需要两步:

      第一:定义一个实现了IHttpHandlerFactory接口的类,并实现其GetHandler方法。在GetHandler中,我们可以根据具体业务选择不同的工厂模式实现方式,下面的代码中包括了通过判断后缀实现的简单工厂模式和通过反射实现的工厂模式两种实现方式。   

HttpHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.IO;
namespace MyHttpHandler {     public class MyHttpHandlerFactory:IHttpHandlerFactory     {         public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)         {             #region 通过判断后缀实现简单工厂模式             ////获取文件服务器物理路径             //string path = context.Request.PhysicalPath;             ////获取文件名后缀名             //string exstention = Path.GetExtension(path).ToLower();
            //if (exstention == ".html")             //{             //    return new HtmlHttpHandler();             //}             //else if (exstention == ".xml")             //{             //    return new XMLHttpHandler();             //}             //else             //{             //    return null;             //}             #endregion
            #region 通过反射实现工厂模式             string handlerName = url.Substring(url.LastIndexOf("/") + 1);             string className = handlerName.Substring(0, handlerName.IndexOf("."));             string fullClassName = "MyHttpHandler." + className;
            object h = null;
            // 采用动态反射机制创建相应的IHttpHandler实现类。             h = Activator.CreateInstance(Type.GetType(fullClassName));
            return (IHttpHandler)h;
            #endregion
        }
        public void ReleaseHandler(IHttpHandler handler)          {                      }     } }
HttpHandler

      第二:在web.config中注册此类     

    <httpHandlers>     <add  verb="*" path="HtmlHttpHandler.html,XMLHttpHandler.xml" type="MyHttpHandler.MyHttpHandlerFactory,MyHttpHandler"/>     </httpHandlers>

     此时,访问HtmlHttpHandler.html和XMLHttpHandler.xml的效果如下:

       HttpHandler

         HttpHandler

四、HttpHandler的实际应用

      因为HttpHandler是Asp.net真正处理请求的地方,所以我们可以在HttpHandler中对请求做一些自定义的处理,甚至彻底改变请求的预期输出。下面列举了几个HttpHandler的应用:

      一、图片防盗链应用。

            有时我们自己web站点中的图片不想被其他站点引用,或者不想让人知道图片地址后直接通过浏览器访问,这就是图片的防盗链。下面是通过HttpHandler实现的图片防盗链:

            

HttpHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web;
namespace MyHttpHandler {     /// <summary>     /// 图片防盗链HttpHandler     /// </summary>     public class PictureHttpHandler : IHttpHandler     {         public bool IsReusable         {             get { return true; }         }
        public void ProcessRequest(HttpContext context)         {             //站点的域名             string myDomain = "localhost";
            if (context.Request.UrlReferrer == null ||                 context.Request.UrlReferrer.Host.ToLower().IndexOf(myDomain) < 0)             {                 //如果是通过浏览器直接访问或者是通过其他站点访问过来的,则显示“资源不存在”图片                 context.Response.ContentType = "image/JPEG";                 context.Response.WriteFile(context.Request.PhysicalApplicationPath+"/images/noimg.jpg");             }             else {                 //如果是通过站内访问的,这正常显示图片                 context.Response.ContentType = "image/JPEG";                 context.Response.WriteFile(context.Request.PhysicalPath);             }                     }     } }
HttpHandler

           把下面的项加到web.config中的httphandler节点中:

           <add  verb="*" path="*.jpg" type="MyHttpHandler.PictureHttpHandler,MyHttpHandler"/>

           正常访问default页面时:

            HttpHandler

             通过图片地址直接访问时:

            HttpHandler

          二、生成验证码

HttpHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Web;
namespace MyHttpHandler {     /// <summary>     /// 验证码Handler     /// </summary>     public class ValidateCodeHttpHandler : IHttpHandler     {         public void ProcessRequest(HttpContext context)         {             context.Response.ContentType = "image/gif";             //建立Bitmap对象,绘图                     Bitmap basemap = new Bitmap(200, 60);             Graphics graph = Graphics.FromImage(basemap);             graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);             Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);             Random r = new Random();             string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";             string letter;             StringBuilder s = new StringBuilder();             //添加随机的五个字母                     for (int x = 0; x < 5; x++)             {                 letter = letters.Substring(r.Next(0, letters.Length - 1), 1);                 s.Append(letter);                 graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));             }             //混淆背景                     Pen linePen = new Pen(new SolidBrush(Color.Black), 2);             for (int x = 0; x < 6; x++)                 graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)),                     new Point(r.Next(0, 199), r.Next(0, 59)));             //将图片保存到输出流中                           basemap.Save(context.Response.OutputStream, ImageFormat.Gif);             //context.Session["CheckCode"] = s.ToString();                //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片                      context.Response.End();         }         public bool IsReusable          {                get { return true; }          }     } }
HttpHandler

           把下面的项加到web.config中的httphandler节点中:

           <add  verb="*" path="validatevode" type="MyHttpHandler.ValidateCodeHttpHandler,MyHttpHandler"/>

           访问validatevode时:

           HttpHandler

源代码:/Files/yourshj/HttpHandler.rar

一、HttpHandler简介

      HttpHandler是asp.net真正处理Http请求的地方。在这个HttpHandler容器中,ASP.NET Framework才真正地对客户端请求的服务器页面做出编译和执行,并将处理过后的信息附加在HTTP请求信息流中再次返回到HttpModule中。

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

     HttpHandlerHttpModule不同,一旦定义了自己的HttpHandler类,那么它对系统的HttpHandler的关系将是“覆盖”关系。

二、IHttpHandler的处理过程

      当一个HTTP请求经过HttpModule容器传递到HttpHandler容器中时,ASP.NET Framework会调用HttpHandler的ProcessRequest成员方法来对这个HTTP请求进行真正的处理。以一个ASPX页面为例,正是在这里一个ASPX页面才被系统处理解析,并将处理完成的结果继续经由HttpModule传递下去,直至到达客户端。

对于ASPX页面,ASP.NET Framework在默认情况下是交给System.Web.UI.PageHandlerFactory这个HttpHandlerFactory来处理的。所谓一个HttpHandlerFactory,是指当一个HTTP请求到达这个HttpHandlerFactory时,HttpHandlerFactory会提供出一个HttpHandler容器,交由这个HttpHandler容器来处理这个HTTP请求。

一个HTTP请求都是最终交给一个HttpHandler容器中的ProcessRequest方法来处理的。

      HttpHandler

三、一个简单的自定义IHttpHandler

      实现一个自定义的IHttpHandler需要两步:

      第一:定义一个实现了IHttpHandler的类,并且实现其ProcessRequest方法。在一个HttpHandler容器中如果需要访问Session,必须实现IRequiresSessionState接口,这只是一个标记接口,没有任何方法。       

HttpHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web;
namespace MyHttpHandler {     public class HelloHttpHandler:IHttpHandler     {         public bool IsReusable          {             get { return true; }         }
        public void ProcessRequest(HttpContext context)          {             context.Response.Write("我是经过HelloHttpHandler处理的!");         }     } }
HttpHandler

      第二:在web.config中注册这个类,并且指定Handler处理的请求类型

               把此节点插入system.web节点中

  <httpHandlers>       <!--path中指定的是执行type中HttpHandler的访问路径。此路径可以带后缀也可以不带后缀。如果path配置为*,则会对所有的请求执行此HttpHandler-->       <add  verb="*" path="httphandlerlearn.html" type="MyHttpHandler.HelloHttpHandler,MyHttpHandler"/>     </httpHandlers>

       这时,访问httphandlerlearn.html就会显示“我是经过HelloHttpHandler处理的!”,如下:

       HttpHandler

五、HttpHandlerFactory

      ASP.NET Framework实际不直接将相关页面的HTTP请求定位到一个其内部默认的IHttpHandler容器之上,而定位到了其内部默认的IHttpHandlerFactory上。IHttpHandlerFactory的作用是对IHttpHandler容器进行调度和管理。

    如果我们自定义的HttpHandler比较多,会在Web.config中注册很多HttpHandler类,这时把这些HttpHandler通过一个HttpHandlerFactory来集中管理就显的非常必要,此时只需要在Web.config中注册此HttphandlerFactory即可。

IHttpHandlerFactory接口包含两个方法。GetHandler返回实现IHttpHandler接口的类的实例,ReleaseHandler使工厂可以重用现有的处理程序实例。

      实现一个自定义的HttphandlerFactory同样需要两步:

      第一:定义一个实现了IHttpHandlerFactory接口的类,并实现其GetHandler方法。在GetHandler中,我们可以根据具体业务选择不同的工厂模式实现方式,下面的代码中包括了通过判断后缀实现的简单工厂模式和通过反射实现的工厂模式两种实现方式。   

HttpHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.IO;
namespace MyHttpHandler {     public class MyHttpHandlerFactory:IHttpHandlerFactory     {         public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)         {             #region 通过判断后缀实现简单工厂模式             ////获取文件服务器物理路径             //string path = context.Request.PhysicalPath;             ////获取文件名后缀名             //string exstention = Path.GetExtension(path).ToLower();
            //if (exstention == ".html")             //{             //    return new HtmlHttpHandler();             //}             //else if (exstention == ".xml")             //{             //    return new XMLHttpHandler();             //}             //else             //{             //    return null;             //}             #endregion
            #region 通过反射实现工厂模式             string handlerName = url.Substring(url.LastIndexOf("/") + 1);             string className = handlerName.Substring(0, handlerName.IndexOf("."));             string fullClassName = "MyHttpHandler." + className;
            object h = null;
            // 采用动态反射机制创建相应的IHttpHandler实现类。             h = Activator.CreateInstance(Type.GetType(fullClassName));
            return (IHttpHandler)h;
            #endregion
        }
        public void ReleaseHandler(IHttpHandler handler)          {                      }     } }
HttpHandler

      第二:在web.config中注册此类     

    <httpHandlers>     <add  verb="*" path="HtmlHttpHandler.html,XMLHttpHandler.xml" type="MyHttpHandler.MyHttpHandlerFactory,MyHttpHandler"/>     </httpHandlers>

     此时,访问HtmlHttpHandler.html和XMLHttpHandler.xml的效果如下:

       HttpHandler

         HttpHandler

四、HttpHandler的实际应用

      因为HttpHandler是Asp.net真正处理请求的地方,所以我们可以在HttpHandler中对请求做一些自定义的处理,甚至彻底改变请求的预期输出。下面列举了几个HttpHandler的应用:

      一、图片防盗链应用。

            有时我们自己web站点中的图片不想被其他站点引用,或者不想让人知道图片地址后直接通过浏览器访问,这就是图片的防盗链。下面是通过HttpHandler实现的图片防盗链:

            

HttpHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web;
namespace MyHttpHandler {     /// <summary>     /// 图片防盗链HttpHandler     /// </summary>     public class PictureHttpHandler : IHttpHandler     {         public bool IsReusable         {             get { return true; }         }
        public void ProcessRequest(HttpContext context)         {             //站点的域名             string myDomain = "localhost";
            if (context.Request.UrlReferrer == null ||                 context.Request.UrlReferrer.Host.ToLower().IndexOf(myDomain) < 0)             {                 //如果是通过浏览器直接访问或者是通过其他站点访问过来的,则显示“资源不存在”图片                 context.Response.ContentType = "image/JPEG";                 context.Response.WriteFile(context.Request.PhysicalApplicationPath+"/images/noimg.jpg");             }             else {                 //如果是通过站内访问的,这正常显示图片                 context.Response.ContentType = "image/JPEG";                 context.Response.WriteFile(context.Request.PhysicalPath);             }                     }     } }
HttpHandler

           把下面的项加到web.config中的httphandler节点中:

           <add  verb="*" path="*.jpg" type="MyHttpHandler.PictureHttpHandler,MyHttpHandler"/>

           正常访问default页面时:

            HttpHandler

             通过图片地址直接访问时:

            HttpHandler

          二、生成验证码

HttpHandler
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.Web;
namespace MyHttpHandler {     /// <summary>     /// 验证码Handler     /// </summary>     public class ValidateCodeHttpHandler : IHttpHandler     {         public void ProcessRequest(HttpContext context)         {             context.Response.ContentType = "image/gif";             //建立Bitmap对象,绘图                     Bitmap basemap = new Bitmap(200, 60);             Graphics graph = Graphics.FromImage(basemap);             graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);             Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);             Random r = new Random();             string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";             string letter;             StringBuilder s = new StringBuilder();             //添加随机的五个字母                     for (int x = 0; x < 5; x++)             {                 letter = letters.Substring(r.Next(0, letters.Length - 1), 1);                 s.Append(letter);                 graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));             }             //混淆背景                     Pen linePen = new Pen(new SolidBrush(Color.Black), 2);             for (int x = 0; x < 6; x++)                 graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)),                     new Point(r.Next(0, 199), r.Next(0, 59)));             //将图片保存到输出流中                           basemap.Save(context.Response.OutputStream, ImageFormat.Gif);             //context.Session["CheckCode"] = s.ToString();                //如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片                      context.Response.End();         }         public bool IsReusable          {                get { return true; }          }     } }
HttpHandler

           把下面的项加到web.config中的httphandler节点中:

           <add  verb="*" path="validatevode" type="MyHttpHandler.ValidateCodeHttpHandler,MyHttpHandler"/>

           访问validatevode时:

           HttpHandler

源代码:/Files/yourshj/HttpHandler.rar