標籤: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實現網頁圖片防盜鏈