1 #region 获取客户端IP地址 2 3 /// <summary> 4 /// 获取客户端IP地址 5 /// </summary> 6 /// <returns></returns> 7 public static string GetIP() 8 { 9 string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 10 if (string.IsNullOrEmpty(result)) 11 { 12 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; 13 } 14 if (string.IsNullOrEmpty(result)) 15 { 16 result = HttpContext.Current.Request.UserHostAddress; 17 } 18 if (string.IsNullOrEmpty(result)) 19 { 20 return "0.0.0.0"; 21 } 22 return result; 23 } 24 25 #endregion 26 27 #region 取客户端真实IP 28 29 /// <summary> 30 /// 取得客户端真实IP。如果有代理则取第一个非内网地址 31 /// </summary> 32 public static string GetIPAddress 33 { 34 get 35 { 36 var result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; 37 if (!string.IsNullOrEmpty(result)) 38 { 39 //可能有代理 40 if (result.IndexOf(".") == -1) //没有“.”肯定是非IPv4格式 41 result = null; 42 else 43 { 44 if (result.IndexOf(",") != -1) 45 { 46 //有“,”,估计多个代理。取第一个不是内网的IP。 47 result = result.Replace(" ", "").Replace("‘", ""); 48 string[] temparyip = result.Split(",;".ToCharArray()); 49 for (int i = 0; i < temparyip.Length; i++) 50 { 51 if (IsIPAddress(temparyip[i]) 52 && temparyip[i].Substring(0, 3) != "10." 53 && temparyip[i].Substring(0, 7) != "192.168" 54 && temparyip[i].Substring(0, 7) != "172.16.") 55 { 56 return temparyip[i]; //找到不是内网的地址 57 } 58 } 59 } 60 else if (IsIPAddress(result)) //代理即是IP格式 61 return result; 62 else 63 result = null; //代理中的内容 非IP,取IP 64 } 65 66 } 67 68 string IpAddress = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null && HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != String.Empty) ? HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] : HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"]; 69 70 if (string.IsNullOrEmpty(result)) 71 result = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"]; 72 73 if (string.IsNullOrEmpty(result)) 74 result = HttpContext.Current.Request.UserHostAddress; 75 76 return result; 77 } 78 } 79 80 #endregion 81 82 #region 判断是否是IP格式 83 84 /// <summary> 85 /// 判断是否是IP地址格式 0.0.0.0 86 /// </summary> 87 /// <param name="str1">待判断的IP地址</param> 88 /// <returns>true or false</returns> 89 public static bool IsIPAddress(string str1) 90 { 91 if (string.IsNullOrEmpty(str1) || str1.Length < 7 || str1.Length > 15) return false; 92 93 const string regFormat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$"; 94 95 var regex = new Regex(regFormat, RegexOptions.IgnoreCase); 96 return regex.IsMatch(str1); 97 } 98 99 #endregion 100 101 #region 获取公网IP 102 /// <summary> 103 /// 获取公网IP 104 /// </summary> 105 /// <returns></returns> 106 public static string GetNetIP() 107 { 108 string tempIP = ""; 109 try 110 { 111 System.Net.WebRequest wr = System.Net.WebRequest.Create("http://city.ip138.com/ip2city.asp"); 112 System.IO.Stream s = wr.GetResponse().GetResponseStream(); 113 System.IO.StreamReader sr = new System.IO.StreamReader(s, System.Text.Encoding.GetEncoding("gb2312")); 114 string all = sr.ReadToEnd(); //读取网站的数据 115 116 int start = all.IndexOf("[") + 1; 117 int end = all.IndexOf("]", start); 118 tempIP = all.Substring(start, end - start); 119 sr.Close(); 120 s.Close(); 121 } 122 catch 123 { 124 if (System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.Length > 1) 125 tempIP = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[1].ToString(); 126 if (string.IsNullOrEmpty(tempIP)) 127 return GetIP(); 128 } 129 return tempIP; 130 } 131 #endregion
时间: 2024-10-14 20:50:35