C# 获取IP地址
2021-03-10 07:28
标签:ace ESS inter oba public 适配器 try reac address using System; namespace Elight.Infrastructure /// /// /// #endregion #region Host(获取主机名) /// /// #endregion #region 获取mac地址 #region Browser(获取浏览器信息) #region 淘宝IP地址库接口模型 public string GetAddress() internal class IpDataEnitiy C# 获取IP地址 标签:ace ESS inter oba public 适配器 try reac address 原文地址:https://www.cnblogs.com/wugh8726254/p/12701391.html
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;
{
///
/// 网络操作。
///
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())
{
return client.DownloadString(url).ToObject
}
}
catch
{
return string.Empty;
}
}
/// 获取主机名。
///
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;
}
///
/// 返回描述本地计算机上的网络接口的对象(网络接口也称为网络适配器)。
///
///
public static NetworkInterface[] NetCardInfo()
{
return NetworkInterface.GetAllNetworkInterfaces();
}
///
/// 通过NetworkInterface读取网卡Mac
///
///
public static List
{
List
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface ni in interfaces)
{
macs.Add(ni.GetPhysicalAddress().ToString());
}
return macs;
}
#endregion
///
/// 获取浏览器信息
///
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
///
/// 淘宝IP地址库接口模型。
/// http://ip.taobao.com/instructions.php
///
internal class TaoBaoIpEnitiy
{
///
/// 响应结果。
///
public int code { get; set; }
///
/// 响应数据。
///
public IpDataEnitiy data { get; set; }
{
if (this.data == null)
{
return string.Empty;
}
return string.Format("{0} {1} {2}", this.data.country, this.data.region, this.data.city);
}
}
{
///
/// 国家
///
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
}
}
上一篇:.NET 单元测试的艺术&单元测试之道C#版
下一篇:C# 获取时间戳