using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Web; namespace CommonHelper { /// /// 网络操作。 /// public class Net { #region Ip(获取Ip) /// /// 获取IP。 /// public static string Ip { get { var result = string.Empty; if (HttpContext.Current != null) { result = GetWebClientIp(); } if (string.IsNullOrEmpty(result)) { result = GetLanIp(); } return result; } } /// /// 获取Web客户端IP。 /// private static string GetWebClientIp() { //HttpContext.Current.Request.UserHostAddress var ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; foreach (var hostAddress in Dns.GetHostAddresses(ip)) { if (hostAddress.AddressFamily == AddressFamily.InterNetwork) return hostAddress.ToString(); } return string.Empty; } /// /// 获取局域网IP。 /// private static string GetLanIp() { foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName())) { if (hostAddress.AddressFamily == AddressFamily.InterNetwork) return hostAddress.ToString(); } return string.Empty; } /// /// 获取IP地址信息(源:淘宝IP库接口)。 /// /// IP /// public static string GetAddress(string ip) { string result = string.Empty; var url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip; try { using (var client = new WebClient()) { client.Encoding = System.Text.Encoding.UTF8; return client.DownloadString(url).ToObject().GetAddress(); } } catch { return string.Empty; } } #endregion #region Host(获取主机名) /// /// 获取主机名。 /// public static string Host { get { return HttpContext.Current == null ? Dns.GetHostName() : GetWebClientHostName(); } } /// /// 获取Web客户端主机名。 /// private static string GetWebClientHostName() { if (!HttpContext.Current.Request.IsLocal) return string.Empty; var ip = GetWebClientIp(); var result = Dns.GetHostEntry(IPAddress.Parse(ip)).HostName; if (result == "localhost.localdomain") result = Dns.GetHostName(); return result; } #endregion #region 获取mac地址 /// /// 返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。 /// /// public static NetworkInterface[] NetCardInfo() { return NetworkInterface.GetAllNetworkInterfaces(); } /// /// 通过NetworkInterface读取网卡Mac /// /// public static List GetMacByNetworkInterface() { List macs = new List(); NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface ni in interfaces) { macs.Add(ni.GetPhysicalAddress().ToString()); } return macs; } #endregion #region Browser(获取浏览器信息) /// /// 获取浏览器信息 /// public static string Browser { get { if (HttpContext.Current == null) return string.Empty; var browser = HttpContext.Current.Request.Browser; return string.Format("{0} {1}", browser.Browser, browser.Version); } } #endregion #region 淘宝IP地址库接口模型 /// /// 淘宝IP地址库接口模型。 /// http://ip.taobao.com/instructions.php /// internal class TaoBaoIpEnitiy { /// /// 响应结果。 /// public int code { get; set; } /// /// 响应数据。 /// public IpDataEnitiy data { get; set; } public string GetAddress() { if (this.data == null) { return string.Empty; } return string.Format("{0} {1} {2}", this.data.country, this.data.region, this.data.city); } } internal class IpDataEnitiy { /// /// 国家 /// public string country { get; set; } /// /// 国家ID /// public string country_id { get; set; } /// /// 地区 /// public string area { get; set; } /// /// 地区ID /// public string area_id { get; set; } /// /// 省份 /// public string region { get; set; } /// /// 省份ID /// public string region_id { get; set; } /// /// 城市 /// public string city { get; set; } /// /// 城市ID /// public string city_id { get; set; } /// /// 地区 /// public string county { get; set; } /// /// 地区ID /// public string county_id { get; set; } /// /// 运营商 /// public string isp { get; set; } /// /// 运营商ID /// public string isp_id { get; set; } /// /// IP /// public string ip { get; set; } } #endregion } }