博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

.net 防止盗链 防止迅雷

Posted on 2011-03-07 13:08  itcfj  阅读(438)  评论(1编辑  收藏  举报

.net 防止盗链 防止迅雷

  • 分类: C#
  • 评论:0
  • 浏览:920

.net 防止盗链 防止迅雷

此程序需要有自己的服务器 或者能够操作 Iis的 IsAPI扩展

我使用的方法就是访问 context.Request.UrlReferrer.Host; 就是访问访问本网站的上一个链接是什么

是否是可以下载网站的Host 如果是可以下载 如果不是 不可以下载!

新建 CeHandler 类 继承 IHttpHandler 接口 实现接口方法

csharp代码
  1.   public class CeHandler:IHttpHandler  
  2.   {  
  3.     #region IHttpHandler 成 员  
  4.   
  5.     public bool IsReusable  
  6.     {  
  7.       get { return true; }  
  8.     }  
  9.   
  10.     public void ProcessRequest(HttpContext context)  
  11.     {  
  12.       string my = string.Empty;  
  13.       my = "jeanwen.com";  
  14.       string host = string.Empty;  
  15.   
  16.       try  
  17.       {  
  18.         host = context.Request.UrlReferrer.Host;  
  19.       }  
  20.       catch (Exception e)  
  21.       {  
  22.         context.Response.End();  
  23.       }  
  24.   
  25.       int xt = host.IndexOf(my);  
  26.       if (xt >= 0)  
  27.       {  
  28.         string url = context.Request.Url.ToString();  
  29.         context.Response.Clear();  
  30.   
  31.         url = url.Replace("http://""");  
  32.         int x = url.IndexOf("/");  
  33.   
  34.   
  35.         if (x > 0)  
  36.         {  
  37.           url = url.Substring(x + 1);  
  38.           string filePath = context.Server.MapPath("~/") + url;  
  39.   
  40.           FileInfo file = new System.IO.FileInfo(filePath);  
  41.   
  42.           context.Response.AddHeader("Content-Disposition""filename=" + file.Name);  
  43.           context.Response.AddHeader("Content-Length", file.Length.ToString());  
  44.   
  45.           context.Response.ContentType = "application/octet-stream";  
  46.           context.Response.WriteFile(file.FullName);  
  47.   
  48.         }  
  49.         context.Response.End();  
  50.       }  
  51.       else  
  52.       {  
  53.          context.Response.End();  
  54.       }  
  55.     }  
  56.   
  57.     #endregion  
  58.   }  
  public class CeHandler:IHttpHandler
  {
    #region IHttpHandler 成员

    public bool IsReusable
    {
      get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
      string my = string.Empty;
      my = "jeanwen.com";
      string host = string.Empty;

      try
      {
        host = context.Request.UrlReferrer.Host;
      }
      catch (Exception e)
      {
        context.Response.End();
      }

      int xt = host.IndexOf(my);
      if (xt >= 0)
      {
        string url = context.Request.Url.ToString();
        context.Response.Clear();

        url = url.Replace("http://", "");
        int x = url.IndexOf("/");


        if (x > 0)
        {
          url = url.Substring(x + 1);
          string filePath = context.Server.MapPath("~/") + url;

          FileInfo file = new System.IO.FileInfo(filePath);

          context.Response.AddHeader("Content-Disposition", "filename=" + file.Name);
          context.Response.AddHeader("Content-Length", file.Length.ToString());

          context.Response.ContentType = "application/octet-stream";
          context.Response.WriteFile(file.FullName);

        }
        context.Response.End();
      }
      else
      {
         context.Response.End();
      }
    }

    #endregion
  }

WebConfig.config 添加

xml代码
  1.   <httpHandlers>  
  2.    <add verb="*" path="*.rar" type="MyChy.Soft.CeHandler, MyChy.Soft"/>  
  3.    <add verb="*" path="*.zip" type="MyChy.Soft.CeHandler, MyChy.Soft"/>  
  4.   </httpHandlers>  
  <httpHandlers>
   <add verb="*" path="*.rar" type="MyChy.Soft.CeHandler, MyChy.Soft"/>
   <add verb="*" path="*.zip" type="MyChy.Soft.CeHandler, MyChy.Soft"/>
  </httpHandlers>

IIS 设置

点击 配置

点击添加

可执行文件 :c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll