zoukankan      html  css  js  c++  java
  • ASP.net 使用HttpHandler实现图片防盗链

    首先是HttpHandler类的代码:

    using System;
    using System.Collections.Generic;
    using System.Web;
    
    namespace HttpHandler
    {
        public class JPEGHandler : IHttpHandler
        {
            #region IHttpHandler 成员
    
         public bool IsReusable
            {
                get { return true; }
            }
    
            public void ProcessRequest(HttpContext context)
            {
                string fileName = context.Request.FilePath;
    
                if (context.Request.UrlReferrer.Host == null)
                {
                    context.Response.ContentType = "image/JPEG";
                    context.Response.WriteFile("/no.jpg");
                }
                else
                {
                    if (context.Request.UrlReferrer.Host.IndexOf("localhost") >= 0)
                    {
                        context.Response.ContentType = "image/JPEG";
                        context.Response.WriteFile(fileName);
                    }
                    else
                    {
                        context.Response.ContentType = "image/JPEG";
                        context.Response.WriteFile("/no.jpg");
                    }
                }
            }
    
            #endregion
        }
    }
    
    

    然后是web.config中的代码:

    <configuration>
    	<system.web>
    		<httpHandlers>
    			<add verb="*" path="*.jpg" type="HttpHandler.JPEGHandler"/>
    		</httpHandlers>
    </configuration>
    最后就是在网站根目录下放入no.jpg就可以了。
  • 相关阅读:
    头部尾部始终处于两端(适用于pc端和移动端)
    运用active和hover实现导航栏的页面切换
    POJ1423-阶乘的位数-Big Number
    大数阶乘
    n皇后
    4103:踩方格
    2815:城堡问题
    特殊回文数
    十六进制转十进制
    十六进制转八进制
  • 原文地址:https://www.cnblogs.com/wubin264/p/1774743.html
Copyright © 2011-2022 走看看