·先创建两个ASP.NET项目,命名,添加Handler.ashx
·在Handler1.ashx里边写
public class Handler1 : IHttpHandler
{
public bool IsReusable
{
get
{
return true;
}
}
}
·再获取上次请求的URL
Uri lastUrl = context.Request.UrlReferrer;
·本次请求的URL
Uri currentUrl = context.Request.Url;
·获取“请勿盗链”警告提示
string errorImagePath = context.Request.PhysicalApplicationPath + "image/qin.jpg";
·发送至客户端
context.Response.WriteFile(errorImagePath);
·判断是否为盗链
f (lastUrl.Host != currentUrl.Host || lastUrl.Port != currentUrl.Port)
{
string errorImagePath = context.Request.PhysicalApplicationPath + "image/1.jpg";
context.Response.WriteFile(errorImagePath);
}
else
{
context.Response.WriteFile(context.Request.PhysicalPath);
}
·在主项目web.config文件里边加入
<system.webServer>
<handlers>
<add verb="" path="image/.jpg" type="T5防盗链.Handler1" name="plink"/>
</handlers>
</system.webServer>
·在主项目中添加一个web窗体,default,及图片
<div>
<img src="image/qin.jpg" />
<img src="image/jiu.jpg" />
</div>
·在辅项目中添加一个web窗体
<p>以下图片来自第一个站点</p>
<img src="https://localhost:44334/image/qin.jpg" />
<img src="https://localhost:44334/image/jiu.jpg" />
·最后将两个项目同时启动,点击解决方案管理,右键点开属性,将单个启动设置为多个,然后启动项目。