string
HostName =
string
.Empty;
string
ip =
string
.Empty;
string
ipv4 = String.Empty;
if
(!
string
.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables[
"HTTP_VIA"
]))
ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables[
"HTTP_X_FORWARDED_FOR"
]);
if
(
string
.IsNullOrEmpty(ip))
ip = Request.UserHostAddress;
foreach
(IPAddress ipAddr
in
Dns.GetHostEntry(ip).AddressList)
{
if
(ipAddr.AddressFamily.ToString() ==
"InterNetwork"
)
{
ipv4 = ipAddr.ToString();
}
}
HostName =
"主机名: "
+ Dns.GetHostEntry(ip).HostName +
" IP: "
+ ipv4;
在本机进行程序代码调试测试,其中字符串ip会显示为::1,是IPv6的IP地址格式,相当于127.0.0.1。最终处理成IPv4的显示地址。
-------------------------------------------------------------------------------------------------------------------------------------------------
上述方法,代码调试没有问题,但是放在服务器上后,客户端访问就出问题了,有部分电脑能获取主机名,但是ipv4显示为空,另外一部分
Dns.GetHostEntry(ip)中的ip正常,但是执行Dns.GetHostEntry(ip)貌似为null,导致出现异常。最后选择的方法是只记录客户端的IPV4,
/// <summary>
/// 获取web客户端ip
/// </summary>
/// <returns></returns>
public static string GetWebClientIp()
{
string userIP = "未获取用户IP";
try
{
if (System.Web.HttpContext.Current == null
|| System.Web.HttpContext.Current.Request == null
|| System.Web.HttpContext.Current.Request.ServerVariables == null)
{
return "";
}
string CustomerIP = "";
//CDN加速后取到的IP simone 090805
CustomerIP = System.Web.HttpContext.Current.Request.Headers["Cdn-Src-Ip"];
if (!string.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
}
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (!String.IsNullOrEmpty(CustomerIP))
{
return CustomerIP;
}
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (CustomerIP == null)
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
}
else
{
CustomerIP = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (string.Compare(CustomerIP, "unknown", true) == 0 || String.IsNullOrEmpty(CustomerIP))
{
return System.Web.HttpContext.Current.Request.UserHostAddress;
}
return CustomerIP;
}
catch { }
return userIP;
}
- public class CheckIP
- {
- #region 获取浏览器版本号
-
-
-
-
-
- public static string GetBrowser()
- {
- HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser;
- return bc.Browser + bc.Version;
- }
-
- #endregion
-
- #region 获取操作系统版本号
-
-
-
-
-
- public static string GetOSVersion()
- {
-
- var userAgent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
-
- var osVersion = "未知";
-
- if (userAgent.Contains("NT 6.1"))
- {
- osVersion = "Windows 7";
- }
- else if (userAgent.Contains("NT 6.0"))
- {
- osVersion = "Windows Vista/Server 2008";
- }
- else if (userAgent.Contains("NT 5.2"))
- {
- osVersion = "Windows Server 2003";
- }
- else if (userAgent.Contains("NT 5.1"))
- {
- osVersion = "Windows XP";
- }
- else if (userAgent.Contains("NT 5"))
- {
- osVersion = "Windows 2000";
- }
- else if (userAgent.Contains("NT 4"))
- {
- osVersion = "Windows NT4";
- }
- else if (userAgent.Contains("Me"))
- {
- osVersion = "Windows Me";
- }
- else if (userAgent.Contains("98"))
- {
- osVersion = "Windows 98";
- }
- else if (userAgent.Contains("95"))
- {
- osVersion = "Windows 95";
- }
- else if (userAgent.Contains("Mac"))
- {
- osVersion = "Mac";
- }
- else if (userAgent.Contains("Unix"))
- {
- osVersion = "UNIX";
- }
- else if (userAgent.Contains("Linux"))
- {
- osVersion = "Linux";
- }
- else if (userAgent.Contains("SunOS"))
- {
- osVersion = "SunOS";
- }
- return osVersion;
- }
- #endregion
-
- #region 获取客户端IP地址
-
-
-
-
-
- public static string GetIP()
- {
- string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
- if (string.IsNullOrEmpty(result))
- {
- result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
- }
- if (string.IsNullOrEmpty(result))
- {
- result = HttpContext.Current.Request.UserHostAddress;
- }
- if (string.IsNullOrEmpty(result))
- {
- return "0.0.0.0";
- }
- return result;
- }
-
- #endregion
-
- #region 取客户端真实IP
-
-
-
-
- public static string GetIPAddress
- {
- get
- {
- var result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
- if (!string.IsNullOrEmpty(result))
- {
-
- if (result.IndexOf(".") == -1)
- result = null;
- else
- {
- if (result.IndexOf(",") != -1)
- {
-
- result = result.Replace(" ", "").Replace("'", "");
- string[] temparyip = result.Split(",;".ToCharArray());
- for (int i = 0; i < temparyip.Length; i++)
- {
- if (IsIPAddress(temparyip[i])
- && temparyip[i].Substring(0, 3) != "10."
- && temparyip[i].Substring(0, 7) != "192.168"
- && temparyip[i].Substring(0, 7) != "172.16.")
- {
- return temparyip[i];
- }
- }
- }
- else if (IsIPAddress(result))
- return result;
- else
- result = null;
- }
-
- }
-
- 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"];
-
- if (string.IsNullOrEmpty(result))
- result = HttpContext.Current.Request.ServerVariables["HTTP_X_REAL_IP"];
-
- if (string.IsNullOrEmpty(result))
- result = HttpContext.Current.Request.UserHostAddress;
-
- return result;
- }
- }
-
- #endregion
-
- #region 判断是否是IP格式
-
-
-
-
-
-
- public static bool IsIPAddress(string str1)
- {
- if (string.IsNullOrEmpty(str1) || str1.Length < 7 || str1.Length > 15) return false;
-
- const string regFormat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$";
-
- var regex = new Regex(regFormat, RegexOptions.IgnoreCase);
- return regex.IsMatch(str1);
- }
-
- #endregion
-
- #region 获取公网IP
-
-
-
-
- public static string GetNetIP()
- {
- string tempIP = "";
- try
- {
- System.Net.WebRequest wr = System.Net.WebRequest.Create("http://city.ip138.com/ip2city.asp");
- System.IO.Stream s = wr.GetResponse().GetResponseStream();
- System.IO.StreamReader sr = new System.IO.StreamReader(s, System.Text.Encoding.GetEncoding("gb2312"));
- string all = sr.ReadToEnd();
-
- int start = all.IndexOf("[") + 1;
- int end = all.IndexOf("]", start);
- tempIP = all.Substring(start, end - start);
- sr.Close();
- s.Close();
- }
- catch
- {
- if (System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.Length > 1)
- tempIP = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList[1].ToString();
- if (string.IsNullOrEmpty(tempIP))
- return GetIP();
- }
- return tempIP;
- }
- #endregion
- }
-
-
-
-
- public string GetRealIP()
- {
- string result = String.Empty;
- result = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
-
- if (!string.IsNullOrWhiteSpace(result))
- {
-
- if (result.IndexOf(".") == -1)
- {
- result = null;
- }
- else
- {
-
- if (result.IndexOf(",") != -1)
- {
- result = result.Replace(" ", string.Empty).Replace("\"", string.Empty);
-
- string[] temparyip = result.Split(",;".ToCharArray());
-
- if (temparyip != null && temparyip.Length > 0)
- {
- for (int i = 0; i < temparyip.Length; i++)
- {
-
- if (IsIPAddress(temparyip[i]) && temparyip[i].Substring(0, 3) != "10." && temparyip[i].Substring(0, 7) != "192.168"&& temparyip[i].Substring(0, 7) != "172.16.")
- {
- return temparyip[i];
- }
- }
- }
- }
-
- else if (IsIPAddress(result))
- {
- return result;
- }
-
- else
- {
- result = null;
- }
- }
- }
-
- if (string.IsNullOrWhiteSpace(result))
- {
- result = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
- }
-
- if (string.IsNullOrWhiteSpace(result))
- {
- result = System.Web.HttpContext.Current.Request.UserHostAddress;
- }
- return result;
- }
- public bool IsIPAddress(string str)
- {
- if (string.IsNullOrWhiteSpace(str) || str.Length < 7 || str.Length > 15)
- return false;
-
- string regformat = @"^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})";
- Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
-
- return regex.IsMatch(str);
- }