.net 中HttpHandler的应用
2010-09-15 12:26
309 查看
HttpHandler 其实就是处理对某种特定文件类型的请求,比如在.net中你可以处理.aspx的页面请求,从而使整个页面处理结果替换输出.
一.请求网站页面的url地址是http的则换成https的
1.建立UrlHander 类实现 IHttpHandler接口
代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace com.migosoft.bj.i5sc.webpages.webUtil
{
public class UrlHander : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Url.ToString().ToLower().Contains("http://"))
{
context.Response.Redirect("https://www.test.com/" + context.Request.Url.AbsolutePath.ToLower().ToString());
}
}
public bool IsReusable
{
get { return true; }
}
}
}
2.Web.config配置文件添加 <httpHandlers>
<httpHandlers>
<add verb="*" path="*.aspx" type="命名空间.UrlHander,程序集名称" />
</httpHandlers>
verb"*"表示对所有(get,post)请求进行处理。Path指明对相应的文件进行处理,"*.aspx"表示会对ASPX页面的请求进行处理
二.asp.net HttpHandler实现图片防盗链
Step.1:创建文件 CustomHandler.cs,代码如下:
using System;
using System.Web;
namespace CustomHandler{
public class JpgHandler : IHttpHandler{
public void ProcessRequest(HttpContext context){
// 获取文件服务器端物理路径
string FileName = context.Server.MapPath(context.Request.FilePath);
// 如果UrlReferrer为空,则显示一张默认的禁止盗链的图片
if (context.Request.UrlReferrer.Host == null){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}else{
// 如果 UrlReferrer中不包含自己站点主机域名,则显示一张默认的禁止盗链的图片
if (context.Request.UrlReferrer.Host.IndexOf("yourdomain.com") > 0){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile(FileName);
}else{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}
}
}
public bool IsReusable{
get{ return true; }
}
}
}
Step.2 编译这个文件 代码如下:
csc /t:library /r:System.Web.dll CustomHandler.cs
Step.3 将编译好的 CustomHandler.dll 拷贝到站点的 Bin 目录下。
Step.4 在Web.Config 中注册这个Handler。
<system.web>
<httpHandlers>
<add path="*.jpg" verb="*" type="CustomHandler.JpgHandler, CustomHandler" />
</httpHandlers>
</system.web>
一.请求网站页面的url地址是http的则换成https的
1.建立UrlHander 类实现 IHttpHandler接口
代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace com.migosoft.bj.i5sc.webpages.webUtil
{
public class UrlHander : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Url.ToString().ToLower().Contains("http://"))
{
context.Response.Redirect("https://www.test.com/" + context.Request.Url.AbsolutePath.ToLower().ToString());
}
}
public bool IsReusable
{
get { return true; }
}
}
}
2.Web.config配置文件添加 <httpHandlers>
<httpHandlers>
<add verb="*" path="*.aspx" type="命名空间.UrlHander,程序集名称" />
</httpHandlers>
verb"*"表示对所有(get,post)请求进行处理。Path指明对相应的文件进行处理,"*.aspx"表示会对ASPX页面的请求进行处理
二.asp.net HttpHandler实现图片防盗链
Step.1:创建文件 CustomHandler.cs,代码如下:
using System;
using System.Web;
namespace CustomHandler{
public class JpgHandler : IHttpHandler{
public void ProcessRequest(HttpContext context){
// 获取文件服务器端物理路径
string FileName = context.Server.MapPath(context.Request.FilePath);
// 如果UrlReferrer为空,则显示一张默认的禁止盗链的图片
if (context.Request.UrlReferrer.Host == null){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}else{
// 如果 UrlReferrer中不包含自己站点主机域名,则显示一张默认的禁止盗链的图片
if (context.Request.UrlReferrer.Host.IndexOf("yourdomain.com") > 0){
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile(FileName);
}else{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("/error.jpg");
}
}
}
public bool IsReusable{
get{ return true; }
}
}
}
Step.2 编译这个文件 代码如下:
csc /t:library /r:System.Web.dll CustomHandler.cs
Step.3 将编译好的 CustomHandler.dll 拷贝到站点的 Bin 目录下。
Step.4 在Web.Config 中注册这个Handler。
<system.web>
<httpHandlers>
<add path="*.jpg" verb="*" type="CustomHandler.JpgHandler, CustomHandler" />
</httpHandlers>
</system.web>
相关文章推荐
- .NET 2.0 泛型在实际开发中的一次小应用 转载(出处:http://www.vipcn.com)
- httpHandler实现.Net无后缀名Web访问
- 使用XML创建Excel文档-.NET教程,XML应用 http://www.z6688.com/info/47612-1.htm
- HTTP在.NET中的一些应用和解析
- 关于HttpHandler与HttpModule的理解和应用方法
- [分享]在自定义的HttpHandler中调用.net默认HttpHandler的方法 转
- HttpHandler应用之 - 防止图片盗链
- Asp.net 中HttpHandler,HttpModule,IHttpHandlerFactory的原理与应用(一)
- HTTP在.NET中的一些应用和解析
- httpHandler实现.Net无后缀名Web访问的实现解析
- [开源应用]利用HTTPHandler+resumableJs+HTML5实现拖拽上传[大]文件
- HttpHandler与HttpModule的理解与应用
- HttpHandler应用之 - 创建图片校验码
- HttpHandler 在SharePoint 2010中的应用
- ASP.Net中自定义Http处理及应用之HttpHandler
- 一起谈.NET技术,用PagePaser创建Page作为HttpHandler
- Flex与.NET互操作(五):使用FileReference+HttpHandler实现文件上传/下载
- Asp.net 中HttpHandler,HttpModule,IHttpHandlerFactory的原理与应用(一)
- Asp.net 中HttpHandler,HttpModule,IHttpHandlerFactory的原理与应用(一)(转)
- .net使用httpHandler添加图片防盗链