1 using System;
2 using System.Web;
3
4 /// <summary>
5 ///DaoLian 的摘要说明
6 /// </summary>
7 public class DaoLian:IHttpHandler
8 {
9 public bool IsReusable
10 {
11 get { return false; }
12 }
13
14 public void ProcessRequest(HttpContext context)
15 {
16 context.Response.ContentType = "image/jpeg";
17 //
18 //当前请求 的地址
19 Uri url = context.Request.Url;
20 Uri urlReferrer = context.Request.UrlReferrer;
21 if (urlReferrer != null)
22 {
23 //判断域名和端口号是否相等
24 if (IsSameDomain(url,urlReferrer))
25 {
26 //获取当前请求的图片的绝对路径
27 string path = context.Request.MapPath(context.Request.RawUrl);
28 context.Response.WriteFile(path);
29 }
30 else
31 {
32 //盗链图片的地址
33 string path = context.Request.MapPath("../daolian.jpg");
34 context.Response.WriteFile(path);
35 }
36 }
37 else
38 {
39 //盗链图片的地址
40 string path = context.Request.MapPath("../daolian.jpg");
41 context.Response.WriteFile(path);
42 }
43 }
44 //判断域名和端口号是否相等
45 bool IsSameDomain(Uri url1,Uri url2)
46 {
47 return Uri.Compare(url1, url2, UriComponents.HostAndPort, UriFormat.Unescaped, StringComparison.CurrentCultureIgnoreCase) == 0;
48 }
49 }