HttpHandler實現網頁圖片防盜鏈

來源:互聯網
上載者:User

標籤:xhtml   erb   ace   return   toe   ges   cti   dha   ref   

 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:IHttpHandler11 {12  13 public bool IsReusable14 {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>

 

 

HttpHandler實現網頁圖片防盜鏈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.