那么我来介绍下图片放盗链的一个方法
首先,添加个httpHandlers请求,WEBCONFIG部分配置节如下:
<httpHandlers>
<add verb="*" path="*.jpg" type="myhandler,WebApplication5"/>
</httpHandlers> 注意:对应于system.web之下,别添错了哦!
然后添加个class,取名为myhandler继承于IHttpHandler
class原型如下:
复制代码 代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public class myhandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string FileName = context.Server.MapPath(context.Request.FilePath);
if (context.Request.UrlReferrer.Host == null)
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("~/images/no.png");//被替换图片
}
else
{
if (context.Request.UrlReferrer.Host.IndexOf("localhost") > -1)//这里是你的域名
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile(FileName);
}
else
{
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile("~/images/no.png");
}
}
}
public bool IsReusable
{
get { return true; }
}
public myhandler()
{
}
}
myhandler继承自IHttpHandler实现了对url来源来判断jpg图片是否被盗链,本class中以localhost为例,修改localhost和images/no.png参数即可