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;
        }
    }

  • 相关阅读:
    潜水员
    混合背包
    多重背包问题
    归并排序——最省时的排序
    HDU 1556 Color the ball
    2016 ACM/ICPC Asia Regional Dalian Online Football Games
    poj 2352 Stars
    poj 2299 Ultra-QuickSort
    关于原码反码补码以及位元算
    2016 湖南省省赛 Problem A: 2016
  • 原文地址:https://www.cnblogs.com/eric-gms/p/3423559.html
Copyright © 2011-2022 走看看