码迷,mamicode.com
首页 > 其他好文 > 详细

获取真ip

时间:2016-04-21 10:13:01      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

  1.       /// <summary>  
  2.         /// 获取真ip  
  3.         /// </summary>  
  4.         /// <returns></returns>  
  5.         public string GetRealIP()  
  6.         {  
  7.             string result = String.Empty;  
  8.   
  9.   
  10.             // result = System.Web.HttpContext.Current.Request.ServerVariables["RemoteIp"];  
  11.             result = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];  
  12.             //可能有代理   
  13.             if (!string.IsNullOrWhiteSpace(result))  
  14.             {  
  15.                 //没有"." 肯定是非IP格式  
  16.                 if (result.IndexOf(".") == -1)  
  17.                 {  
  18.                     result = null;  
  19.                 }  
  20.                 else  
  21.                 {  
  22.                     //有",",估计多个代理。取第一个不是内网的IP。  
  23.                     if (result.IndexOf(",") != -1)  
  24.                     {  
  25.                         result = result.Replace(" ", string.Empty).Replace("\"", string.Empty);  
  26.                         string[] temparyip = result.Split(",;".ToCharArray());  
  27.                         if (temparyip != null && temparyip.Length > 0)  
  28.                         {  
  29.                             for (int i = 0; i < temparyip.Length; i++)  
  30.                             {  
  31.                                 //找到不是内网的地址  
  32.                                 if (IsIPAddress(temparyip[i]) && temparyip[i].Substring(0, 3) != "10." && temparyip[i].Substring(0, 7) != "192.168" && temparyip[i].Substring(0, 7) != "172.16.")  
  33.                                 {  
  34.                                     return temparyip[i];  
  35.                                 }  
  36.                             }  
  37.                         }  
  38.                     }  
  39.                     //代理即是IP格式  
  40.                     else if (IsIPAddress(result))  
  41.                     {  
  42.                         return result;  
  43.                     }  
  44.                     //代理中的内容非IP  
  45.                     else  
  46.                     {  
  47.                         result = null;  
  48.                     }  
  49.                 }  
  50.             }  
  51.             if (string.IsNullOrWhiteSpace(result))  
  52.             {  
  53.                 result = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];  
  54.             }  
  55.             if (string.IsNullOrWhiteSpace(result))  
  56.             {  
  57.                 result = System.Web.HttpContext.Current.Request.UserHostAddress;  
  58.             }  
  59.             return result;  
  60.         }  
  61.         public bool IsIPAddress(string str)  
  62.         {  
  63.             if (string.IsNullOrWhiteSpace(str) || str.Length < 7 || str.Length > 15)  
  64.                 return false;  
  65.   
  66.   
  67.             string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}";  
  68.             //var match = Regex.Match(str, @"^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})");  
  69.             Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);  
  70.   
  71.   
  72.             return regex.IsMatch(str);  
  73.         } 

获取真ip

标签:

原文地址:http://www.cnblogs.com/panjiayuan/p/5415559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!