zoukankan      html  css  js  c++  java
  • 网站防盗链就是那么简单

    第一步 : 实现 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>
  • 相关阅读:
    pug模板引擎(原jade)之 分支条件
    pug模板引擎(原jade)之 属性
    hosts的作用
    typeScript 之 (7) 构造函数和this
    typeScript 之 (8) 继承,重写,super
    typeScript 之(6) 类的简介
    MySQL:一条更新语句是如何执行的
    Centos7 使用 Kubeadm 搭建 k8s 集群
    HTTP Keep-Alive模式客户端与服务器如何判定传输完成
    MySQL:一条SQL是如何执行的
  • 原文地址:https://www.cnblogs.com/rhythmK/p/2476192.html
Copyright © 2011-2022 走看看