您的位置:首页 > 理论基础 > 计算机网络

ASP.NET基础 HttpHandler

2014-02-20 09:53 267 查看
概念:

HtttpHandler是HTTP请求的处理中心,也正是在这个HttpHandler容器中,ASP.NET
Framework才真正地对客户端请求的服务器页面做出编译和执行,

并将处理过后的信息附加在HTTP请求信息流中再次返回到HttpModule中。

IHttpHandler:

IhttpHandler定义了处理HTTP请求所必须实现的接口。HttpHandler与HttpModule不同,一旦定义了自己的HttpHandler类,

它将覆盖系统的HttpHandler。

IHttpHandler如何处理Http请求:

当一个HTTP请求经HttpModule容器传递到HttpHandler容器中时,ASP.NET
Framework会调用HttpHandler的ProcessRequest成员方法

来对这个HTTP请求进行真正的处理。以一个ASPX页面为例,正是在这里一个ASPX页面才被系统处理解析,并将处理完成的结果

继续经由HttpModule传递下去,直至到达客户端。

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



HttpHandler实例:

通过实现IHttpHandler接口可以创建自定义HTTP处理程序,该接口只包含两个方法。通过调用IsReusable,IHttpHandlerFactory
可以查询处理程序以确定是否可以使用同一实例为多个请求提供服务。ProcessRequest方法将HttpContext实例用作参数,
这使它能够访问Request和Response内部对象。在一个HttpHandler容器中如果需要访问Session,必须实现IRequiresSessionState接口,
这只是一个标记接口,没有任何方法。

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace IHttpHandlerDemo
{
public partial class HttpHandlerDemo : IHttpHandler, IRequiresSessionState
{
#region IHttpHandler成员
public bool IsReusable
{
get { return true; }
}

public void ProcessReqest(HttpContext context)
{
context.Response.Write("这是一个HttpHandler");
context.Session["test"] = "session的内容";//只有继承了IRequiresSessionState才能够使用session
}

#endregion
}
}


在web.config中还需加入如下配置:

<?xml version="1.0" encoding="utf-8"?>

<!--

有关如何配置 ASP.NET 应用程序的详细消息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
-->

<configuration>

<system.web>

<compilation debug="true" targetFramework="4.0" />

<httpHandlers>

<add verb="*" path="*" type="IHttpHandlerDemo.HttpHandlerDemo, IHttpHandlerDemo"/>

</httpHandlers>

</system.web>

</configuration>

IHttpHandlerFactory接口

ASP.NET Framework实际不直接将相关的页面资源HTTP请求定位到一个其内部默认的IHttpHandler容器之上,而定位到了其内部默认的IHttpHandler工厂上。
IHttpHandler工厂的作用是对IHttpHandler容器进行调度和管理。
IHttpHandlerFactory接口包含两个方法。GetHandler返回实现IHttpHandler接口的类的实例,ReleaseHandler使工厂可以重用现有的处理程序实例。
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace MyHandler
{
public class MyHandlerFactory : IHttpHandlerFactory
{
#region IHttpHandlerFactory 成员

public IHttpHandler GetHandler(HttpContext context, string requestType,
string url, string pathTranslated)
{
string fname = url.Substring(url.IndexOf('/') + 1);
while (fname.IndexOf('/') != -1)
fname = fname.Substring(fname.IndexOf('/') + 1);
string cname = fname.Substring(0, fname.IndexOf('.'));
string className = "MyHandler." + cname;

object h = null;

try
{
// 采用动态反射机制创建相应的IHttpHandler实现类。
h = Activator.CreateInstance(Type.GetType(className));
}
catch (Exception e)
{
throw new HttpException("工厂不能为类型"+cname+"创建实例。",e);
}

return (IHttpHandler)h;
}

public void ReleaseHandler(IHttpHandler handler)
{

}

#endregion
}

public class Handler1 : IHttpHandler
{
#region IHttpHandler 成员

public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
context.Response.Write("<html><body><h1>来自Handler1的信息。</h1></body></html>");
}

#endregion
}

public class Handler2 : IHttpHandler
{
#region IHttpHandler 成员

public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
context.Response.Write("<html><body><h1>来自Handler2的信息。</h1></body></html>");
}

#endregion
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: