其他分享
首页 > 其他分享> > HttpHandler的使用

HttpHandler的使用

作者:互联网

1:原网页界面:
        <div>
            <img src="images/adv1.jpg"/>
            <img src="images/adv2.jpg"/>
            <img src="images/adv3.jpg"/>
        </div>

默认 images/adv1.jpg  的默认图片地址:https://localhost:4413/images/adv1.jpg

 

2:盗取网页界面:

        <div>
            <img src="https://localhost:4413/images/adv2.jpg"/>
        </div>

盗取图片src指向  https://localhost:4413/images/adv2.jpg

 

3:在原网页的web.config配置文件中添加

 

<system.webServer>
      <handlers>
            <add verb="*" path="images/*.jpg" name="PreventLink" type="WebApplication3.TestHttpHandler"/>
      </handlers>
  </system.webServer>

 

针对于原网页所有图片的jpg类型的文件

 

 

4:在原网页创建TestHttpHandler类:TestHttpHandler:IHttpHandler

namespace WebApplication3
{
    public class TestHttpHandler:IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }
        public void ProcessRequest(HttpContext context)
        {
            //获取上次请求的url
            Uri lastUrl=context.Request.UrlReferrer;
            //获取本次请求的Url
            Uri currrentUrl=context.Request.Url;
            //判断是否为盗链
            if (lastUrl.Host!=currrentUrl.Host|| lastUrl.Port!=currrentUrl.Port)
            {
                //获取“请勿盗链”警告体会是图片路径
                string errorImagePath = context.Request.PhysicalApplicationPath + "Error/default.jpg";
                //发送至客户端
                context.Response.Write(errorImagePath);
            }
            else
            {
                context.Response.WriteFile(context.Request.PhysicalPath);
            }
        }
    }
}

 

标签:网页,Request,jpg,context,使用,images,public,HttpHandler
来源: https://www.cnblogs.com/dz-zs0706/p/16164710.html