网络(NET)帮助类
public class Net { /// <summary> /// 获取Ip /// </summary> public static string Ip { get { string text = string.Empty; bool flag = HttpContext.Current != null; if (flag) { text = Net.GetWebClientIp(); } bool flag2 = string.IsNullOrWhiteSpace(text); if (flag2) { text = Net.GetLanIp(); } return text; } } /// <summary> /// 获取主机名 /// </summary> public static string Host { get { return (HttpContext.Current == null) ? Dns.GetHostName() : Net.GetWebClientHostName(); } } /// <summary> /// 获取浏览器信息 /// </summary> public static string Browser { get { bool flag = HttpContext.Current == null; string result; if (flag) { result = string.Empty; } else { HttpBrowserCapabilities browser = HttpContext.Current.Request.Browser; result = string.Format("{0} {1}", browser.Browser, browser.Version); } return result; } } /// <summary> /// 获取Web客户端的Ip /// </summary> private static string GetWebClientIp() { string webRemoteIp = Net.GetWebRemoteIp(); IPAddress[] hostAddresses = Dns.GetHostAddresses(webRemoteIp); string result; for (int i = 0; i < hostAddresses.Length; i++) { IPAddress iPAddress = hostAddresses[i]; bool flag = iPAddress.AddressFamily == AddressFamily.InterNetwork; if (flag) { result = iPAddress.ToString(); return result; } } result = string.Empty; return result; } /// <summary> /// 获取Web远程Ip /// </summary> private static string GetWebRemoteIp() { return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } /// <summary> /// 获取局域网IP /// </summary> private static string GetLanIp() { IPAddress[] hostAddresses = Dns.GetHostAddresses(Dns.GetHostName()); string result; for (int i = 0; i < hostAddresses.Length; i++) { IPAddress iPAddress = hostAddresses[i]; bool flag = iPAddress.AddressFamily == AddressFamily.InterNetwork; if (flag) { result = iPAddress.ToString(); return result; } } result = string.Empty; return result; } /// <summary> /// 获取Web客户端主机名 /// </summary> private static string GetWebClientHostName() { bool flag = !HttpContext.Current.Request.IsLocal; string result; if (flag) { result = string.Empty; } else { string webRemoteIp = Net.GetWebRemoteIp(); string hostName = Dns.GetHostEntry(IPAddress.Parse(webRemoteIp)).HostName; bool flag2 = hostName == "localhost.localdomain"; if (flag2) { hostName = Dns.GetHostName(); } result = hostName; } return result; } }