2021-05-31

实现防盗链

首先创建第一个网站T5.3。
在这里插入图片描述
在桌面上新建一个文件夹,命名为image。托几张jpg的图片。
在这里插入图片描述
然后把文件夹托入网站中。
在这里插入图片描述
然后添加一个aspx页面,插入网站提供的图片路径。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowImage1.aspx.cs" Inherits="T5._3.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    </head>
<body>
 <form id="form1" runat="server">
        <div>
         //创建被盗链站点
            <img src="Images/1.jpg" />
             <img src="Images/2.jpg" />
              <img src="Images/3.jpg" />
                </div>
    </form>
</body>
</html>

执行页面结果图。
在这里插入图片描述
创建第二个网站T5.1。
,添加aspx页面,引用第一个网站的jpg图片资源,用的是图片的绝对地址(在浏览第一个网站的页面时,基于图片右键单击,弹出选项框,选择复制图片地址)。代码。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowImage2.aspx.cs" Inherits="T5._1.ShowImages" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
<form id="form1" runat="server">
        <div>
           这三张图片是从第一个网站,把他盗取过来的
            //创建盗链站点
             <img src="https://localhost:44318/Images/1.jpg" />
             <img src="https://localhost:44318/Images/2.jpg" />
             <img src="https://localhost:44318/Images/3.jpg" />
           </div>
    </form>
</body>
</html>
            

防盗链操作
在第一个网站中添加HttpHandler类。代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace T5._3
{
  /// <summary>
    /// PreventLink 的摘要说明
    /// </summary>
     public class PreventLink : IHttpHandler
    {
     public bool IsReusable
        {
            get { return true; }
        }
         public void ProcessRequest(HttpContext context)
        {
            //获取上次请求的URL
             Uri lastUrl = context.Request.UrlReferrer;
            //获取本次请求的URL
               Uri currentUrl = context.Request.Url;
            //判断是否为盗链
             if (lastUrl.Host != currentUrl.Host || lastUrl.Port != currentUrl.Port)
            {
             //获取“请勿盗链”警告提示图片路径
                string errorImagePath = context.Request.PhysicalApplicationPath + "Images/1.jpg";
                   context.Response.WriteFile(errorImagePath);
            }
            else
              {
                context.Response.WriteFile(context.Request.PhysicalPath);
            }
             }
             }
             }

在Web.config中配置,在节点下添加以下内容。

 <system.webServer>
    <handlers>
      <add verb="*" path="images/*.jpg" type="T5._3.PreventLink" name="plink"/>
    </handlers>
    </system.webServer>

实现防盗链效果。
在这里插入图片描述

图片添加水印

创建一个网站,将带有图片文件夹添加至网站中。
在这里插入图片描述
在网站中添加HttpHandler类,继承IHttpHandler接口,编写加水印功能,代码如下。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
namespace T5
{
    public class HttpHandler : IHttpHandler
    {
    //水印图片
      private const string WATERMARK_URL = "img/2923.jpg";
      //水印不存在,默认显示的图片
        private const string DEFAULTIMAGE_URL = "img/proxy(1).jpg";
         public void ProcessRequest(HttpContext context)
        {
        //得到用户请求的图片
         string filePath = context.Request.PhysicalPath;//请求路径
            System.Drawing.Image Cover;
            //判断请求的物理路径中是否存在文件
              if (File.Exists(filePath))
            {
            //加载文件
             Cover = Image.FromFile(filePath);
             //加载水印图片
                Image watermark = Image.FromFile(context.Server.MapPath(WATERMARK_URL));
                //实例化图片
                 Graphics g = Graphics.FromImage(Cover);
                 //在image上绘制水印
                 g.DrawImage(watermark, new Rectangle(Cover.Width - watermark.Width,
            Cover.Height - watermark.Height, watermark.Width, watermark.Height), 0,0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);
            //释放画布
                g.Dispose();
                //释放水印图片           
                      watermark.Dispose();
            }
             else
            {
            //加载默认图片
               Cover = Image.FromFile(context.Request.MapPath(DEFAULTIMAGE_URL));
            }
            //设置输出格式
     context.Response.ContentType = "image/jpeg";
     //将图片存入输出流
            Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);        
              Cover.Dispose();
            context.Response.End();
             }
              public bool IsReusable
        {
            get { return false; }
        }
          }
}

在Web.config中配置实现当请求图片资源时有HttpHandler类处理。

 <system.webServer>
    <handlers>
      <add verb="*" path="images/*.jpg" type="T5.PreventLink" name="handle"/>
    </handlers>
  </system.webServer>

添加Web页面,展示图片。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="T5.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
 <form id="form1" runat="server">
        <div>
         <img src="img/2923.jpg" />
            <img src="img/proxy (1).jpg" />
               </div>
    </form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值