文件窗体
这是通过直接获取本地图片位置进行上传。
<asp:Image ID="Image1" runat="server" src="images/adv1.jpg" />
<asp:Image ID="Image2" runat="server" src="images/adv2.jpg" />
<asp:Image ID="Image3" runat="server" src="images/adv3.jpg" />
盗取窗体
通过盗取图片地址进行上传,不需要本地图片。
<p>以下图片资源来自第一个网站</p>
<asp:Image ID="Image1" runat="server" src="http://localhost:49310/images/adv1.jpg" />
<asp:Image ID="Image2" runat="server" src="http://localhost:49310/images/adv2.jpg" />
<asp:Image ID="Image3" runat="server" src="http://localhost:49310/images/adv3.jpg" />
快捷创建
可以通过创建一般处理程序获取快捷模块
这是代码段
public class PreventLink1 : IHttpHandler
{
public bool IsReusable
{
get
{
return true;
}
}
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
//获取上次请求的URL
Uri lastUrl = context.Request.UrlReferrer;
//URL获取本次请求的URL
Uri currenturl = context.Request.Url;
//判断是否为盗
if (lastUrl.Host != currenturl.Host || lastUrl.Port != currenturl.Port)
{
//获取“请勿盗链”警告提示图片路径
string errorlmagePath = context.Request.PhysicalApplicationPath + "sa/adv4.jpg";
//发送至客户端
context.Response.WriteFile(errorlmagePath);
}
else
{
context.Response.WriteFile(context.Request.PhysicalPath);
}
}```
类别手写
除了上述方法外,还可以创建类,用手写的方式实现效果。
public class naotan:IHttpHandler
{
public bool IsReusable => true;
public void ProcessRequest(HttpContext context)
{
//获取上次请求的URL
Uri lastUrl = context.Request.UrlReferrer;
//URL获取本次请求的URL
Uri currenturl = context.Request.Url;
//判断是否为盗
if (lastUrl.Host != currenturl.Host || lastUrl.Port != currenturl.Port)
{
//获取“请勿盗链”警告提示图片路径
string errorlmagePath = context.Request.PhysicalApplicationPath + "sa/adv4.jpg";
//发送至客户端
context.Response.WriteFile(errorlmagePath);
}
else
{
context.Response.WriteFile(context.Request.PhysicalPath);
}
}
调用
做完以上代码之后,我们需要对其进行调用
首先我们先在需要防盗的项目中找到Web.config窗体
之后再当中加入代码进行调用
<system.webServer>
<handlers>
<add verb="*" path="images/*" //表示获取位置,*表示任意
type="T5._1.PreventLink1" //表示对应的项目中的类
name="lnit"/> //名字,随意起
</handlers>
</system.webServer>