HttpHanlder 处理图片防盗链【源码】
解释:自己网站上的图片不想让别人引用,如果别人把自己网站上的图片的链接放在别人的网站上
之后,那么访问别人网站的时候,就会请求自己网站上的这个图片。这里就用到了HttpHandler,
检查*.jpg文件(请求自己的网站时),如果请求域名时自己的那么就正常显示,否则就是显示error.jpg
对jpg文件的请求进行拦截 过滤!
-------------------------------
附代码+demo下载地址
--------------------------------
1:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="httphandler._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="0001.jpg" />
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src="0001.jpg" />
</div>
</form>
</body>
</html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace httphandler
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : 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.Contains("localhost"))
{ 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 false;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace httphandler
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : 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.Contains("localhost"))
{ 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 false;
}
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
<add path="*.jpg" verb="*" type="httphandler.Handler1, httphandler" />
</httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
<add path="*.jpg" verb="*" type="httphandler.Handler1, httphandler" />
</httpHandlers>
关于web.config,只是新添加了<add path="*.jpg" verb="*" type="httphandler.Handler1, httphandler" />
其中 path指对于哪种类型文件的请求,verb访问方式,type:由谁处理
总起来就是当某个页面上有对jpg文件的请求(含有一个图片)的时候, 无论以什么方式(或者是post或者是get……),都先运行httphandler 程序集下的httphandler.Handler1的类来处理这个特殊请求
2:
httphandler图片防盗链.rar
下载地址:http://u.115.com/file/f34ef9cb0a
posted on 2010-06-25 13:29 Master zhu 阅读(504) 评论(2) 编辑 收藏 举报