• 网站防盗链就是那么简单


    第一步 : 实现 IHttpHandler 接口

        

    namespace WebHotlinkProtection
    {

        public class HotlinkProtectionHandler:IHttpHandler
        {
            public bool IsReusable
            {
                get { throw new NotImplementedException(); }
            }

            public void ProcessRequest(HttpContext context)
            {
                //监听是否本站发起的请求
               if (!context.Request.UrlReferrer.Host.StartsWith("localhost"))
                {
                    context.Response.Expires = 0;
                    context.Response.Clear();
                    context.Response.ContentType = "image/jpg";
                    //输出防盗链图片
                    context.Response.WriteFile(context.Request.PhysicalApplicationPath + "\\no.jpg");
                    context.Response.End();
                }
                else
               { 
                    context.Response.Expires = 0;
                    context.Response.Clear();
                    context.Response.ContentType = "image/jpg";
                    context.Response.WriteFile(context.Request.PhysicalPath);
                    context.Response.End();
                }
            }
        }
    }

    第二部:配置web.config

          <httpHandlers>
              <add verb="*" path="*.jpg" type="WebHotlinkProtection.HotlinkProtectionHandler,WebHotlinkProtection"/>
          </httpHandlers>
  • 相关阅读:
    POJ 3041 Asteroids 二分图匹配
    ZOJ 3705 Applications 模拟
    UNIX环境高级编程(第3版)
    明清美文四卷本(共四册)
    卑鄙的圣人:曹操(全10册)
    爱丽丝梦游仙境
    我在大清官场30年
    乌合之众:大众心理研究
    Java多线程编程实战指南
    Linux就该这么学
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2476192.html
Copyright © 2020-2023  润新知