zoukankan      html  css  js  c++  java
  • 图片防盗链技术

    using System;
    using System.Web;


    /// <summary>
    /// ImageProtect 的摘要说明
    /// </summary>
    public class ImageProtect : IHttpHandler
    {
        public ImageProtect()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public void ProcessRequest(HttpContext context)
        {
            //判断是否是本地引用,如果是则返回给客户端正确的图片,这里的判断就是用到了前面所述的http请求中所记路的参考页信息
            //此处使用了端口,仅用于      
            if (context.Request.UrlReferrer.Host == "localhost"&&context.Request.UrlReferrer.Port==6999)              
            {
                //设置客户端缓冲中文件过期时间为0,即立即过期。
                context.Response.Expires = 0;                              
                //清空服务器端为此会话开辟的输出缓存
                context.Response.Clear();                                  
                //获得文件类型
                context.Response.ContentType = "image/jpeg";               
                //将请求文件写入到服务器端为此会话开辟的输出缓存中
                context.Response.WriteFile(context.Request.PhysicalPath);  
                //将服务器端为此会话开辟的输出缓存中的信息传送到客户端
                context.Response.End();  
            }
            else     //如果不是本地引用,则属于盗链引用,返回给客户端错误的图片
            {
                //设置客户端缓冲中文件过期时间为0,即立即过期。
                context.Response.Expires = 0;                              
                //清空服务器端为此会话开辟的输出缓存
                context.Response.Clear();                                  
                //获得文件类型
                context.Response.ContentType = "image/jpeg";               
                //将特殊的报告错误的图片文件写入到服务器端为此会话开辟的输出缓存中
                context.Response.WriteFile("~/images/error.jpg");                   
                //将服务器端为此会话开辟的输出缓存中的信息传送到客户端
                context.Response.End();
            }

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }



    Web.Config
    <httpHandlers>
     <add verb="*" path="*.jpg" type="ImageProtect"/>
    </httpHandlers>
  • 相关阅读:
    window.parent 、window.top及window.self 详解
    js中的变量提升和函数提升
    IE不支持ES6语法的解决方案——Babel
    JavaScript 文件拖拽上传插件 dropzone.js 介绍
    C# DataTable 增加行与列
    group by 与 order by 一起使用的时候
    window.open传递多个参数
    Jquery EasyUI tree 的异步加载(遍历指定文件夹,根据文件夹内的文件生成tree)
    ASP.NET中调用百度地图API
    C# 读取Excel中的数据到DataTable中
  • 原文地址:https://www.cnblogs.com/lds85930/p/1193886.html
Copyright © 2011-2022 走看看