HttpHandler的使用
1:原网页界面:
<div> <img src="images/adv1.jpg"/> <img src="images/adv2.jpg"/> <img src="images/adv3.jpg"/> </div>
默认 images/adv1.jpg 的默认图片地址:https://localhost:4413/images/adv1.jpg
2:盗取网页界面:
<div> <img src="https://localhost:4413/images/adv2.jpg"/> </div>
盗取图片src指向 https://localhost:4413/images/adv2.jpg
3:在原网页的web.config配置文件中添加
<system.webServer> <handlers> <add verb="*" path="images/*.jpg" name="PreventLink" type="WebApplication3.TestHttpHandler"/> </handlers> </system.webServer>
针对于原网页所有图片的jpg类型的文件
4:在原网页创建TestHttpHandler类:TestHttpHandler:IHttpHandler
namespace WebApplication3
{
public class TestHttpHandler:IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
//获取上次请求的url
Uri lastUrl=context.Request.UrlReferrer;
//获取本次请求的Url
Uri currrentUrl=context.Request.Url;
//判断是否为盗链
if (lastUrl.Host!=currrentUrl.Host|| lastUrl.Port!=currrentUrl.Port)
{
//获取“请勿盗链”警告体会是图片路径
string errorImagePath = context.Request.PhysicalApplicationPath + "Error/default.jpg";
//发送至客户端
context.Response.Write(errorImagePath);
}
else
{
context.Response.WriteFile(context.Request.PhysicalPath);
}
}
}
}
每一个成功的背后都有一段不为人知的故事