zoukankan      html  css  js  c++  java
  • .net学习图片防盗链

    代码实现如下

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    ///全局处理程序
    ///1、新建类,实现IHttpHandler
    ///2、实现相应的功能
    ///3、配置web.config
     <httpHandlers>
          <!-- type="命名空间.类名,程序集的名字"-->
          <add verb="*"《请求类型 get post》 path="img/*.jpg" 《那个路径下的图片》 type="DaoLian"《类名》/>
        </httpHandlers>
    /// </summary>
    public class DaoLian:IHttpHandler
    {

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            //当前请求
            Uri u1 = context.Request.Url;
            //上一次请求
            Uri u2 = context.Request.UrlReferrer;

            //比较域名和端口是否一致
            if (CompareUrl(u1, u2))
            {
                //正常的请求
                string path = context.Request.RawUrl;
                path = context.Request.MapPath(path);
                context.Response.WriteFile(path);
            }
            else
            {
                //盗链的请求
                //相对路径 相对于浏览器当前请求的文件
                string path = context.Request.MapPath("daolian.jpg");
                context.Response.WriteFile(path);
            }
        }
        //比较两个uri的域名和端口是否相等
        bool CompareUrl(Uri u1, Uri u2)
        {
            return Uri.Compare(u1, u2, UriComponents.HostAndPort, UriFormat.SafeUnescaped, StringComparison.CurrentCultureIgnoreCase) == 0;
        }
    }

  • 相关阅读:
    jquery easy ui 简单字段选择搜索实现
    (转)EasyUI 分页总结
    EasyUI 搜索框
    微信公众号开发简单介绍
    【POJ3740】Easy Finding DLX(Dancing Links)精确覆盖问题
    推断View是否显示在界面上
    菜鸟调错(八)—— Maven编译错误:不兼容的类型的解决方式
    js对table操作(添加删除交换上下TR)
    NBUT 1222 English Game(trie树+DP)
    Android 返回键的处理
  • 原文地址:https://www.cnblogs.com/eric-gms/p/3423559.html
Copyright © 2011-2022 走看看