• HttpHandler实现网页图片防盗链


     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Web;
     5  
     6 /// <summary>
     7 /// HotLinkedHandler 的摘要说明
     8 ///1.后台代码
     9 /// </summary>
    10 public class HotLinkedHandler:IHttpHandler
    11 {
    12  
    13 public bool IsReusable
    14 {
    15 get { return false; }
    16 }
    17  
    18 public void ProcessRequest(HttpContext context)
    19 {
    20 //得到默认图片
    21 string defaultImg = context.Server.MapPath("~/images/BookCovers/default.jpg");
    22 //得到图片路径
    23 string bookImg = context.Request.PhysicalPath;
    24  
    25 if (context.Request.UrlReferrer.Host == "location" && context.Request.UrlReferrer.Port == context.Request.Url.Port)
    26 {
    27 context.Response.WriteFile(bookImg);
    28 }
    29 else {
    30 context.Response.WriteFile(defaultImg);
    31 }
    32  
    33 context.Response.End();
    34 }
    35 }
     
    在web.config中进行配置
    1 <system.webServer>
    2 <!--path:图片路径,type:指定处理程序类,verb:谓词 get post ftp等 *匹配所有,name:名称-->
    3 <handlers>
    4 <!--配置防盗链-->
    5 <add type="HotLinkedHandler" path="images/BookCovers/*.jpg" name="hotLinked" verb="*"/>
    6 </handlers>
    7 </system.webServer>
     
     
     
     
    //2.前台代码
     1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestWaterImgSecound.aspx.cs" Inherits="TestWaterImg" %>
     2  
     3 <!DOCTYPE html>
     4  
     5 <html xmlns="http://www.w3.org/1999/xhtml">
     6 <head runat="server">
     7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     8 <title></title>
     9 </head>
    10 <body>
    11 <form id="form1" runat="server">
    12 <div>
    13 <img src="images/BookCovers/7111171144.jpg" /><img src="images/BookCovers/7113054846.jpg" />
    14 </div>
    15 </form>
    16 </body>
    17 </html>
     
     
     
    //3.另一个网站引用图片路径
     
     1 <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
     2  
     3 <!DOCTYPE html>
     4  
     5 <html xmlns="http://www.w3.org/1999/xhtml">
     6 <head runat="server">
     7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     8 <title></title>
     9 </head>
    10 <body>
    11 <form id="form1" runat="server">
    12 <div>
    13 <img src="http://localhost:22247/images/BookCovers/7111171144.jpg" />
    14 </div>
    15 </form>
    16  
    17 </body>
    18 </html>
     
    作者:JamelAr
    个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

    本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    HDU4758 Walk Through Squares(AC自动机+状压DP)
    HIT2543 Stone IV(一定费用内的最大流)
    HIT2715 Matrix3(最小费用最大流)
    COGS738 [网络流24题] 数字梯形(最小费用最大流)
    HDU3157 Crazy Circuits(有源汇流量有上下界网络的最小流)
    ZOJ3229 Shoot the Bullet(有源汇流量有上下界网络的最大流)
    BZOJ 1834 [ZJOI2010]network 网络扩容(费用流)
    BZOJ 1475 方格取数(二分图最大点权独立集)
    BZOJ 4236 JOIOJI(前缀和)
    HZAU 1201 Friends(树形DP)
  • 原文地址:https://www.cnblogs.com/JamelAr/p/7002867.html
Copyright © 2020-2023  润新知