.net 防止盗链 防止迅雷
时间:2011-03-07 来源:itcfj
此程序需要有自己的服务器 或者能够操作 Iis的 IsAPI扩展
我使用的方法就是访问 context.Request.UrlReferrer.Host; 就是访问访问本网站的上一个链接是什么
是否是可以下载网站的Host 如果是可以下载 如果不是 不可以下载!
新建 CeHandler 类 继承 IHttpHandler 接口 实现接口方法
csharp代码- 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
- }
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代码- <httpHandlers>
- <add verb="*" path="*.rar" type="MyChy.Soft.CeHandler, MyChy.Soft"/>
- <add verb="*" path="*.zip" type="MyChy.Soft.CeHandler, MyChy.Soft"/>
- </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
相关阅读 更多 +