这个IP库实测还是比较准确的,免费版的可以具体到国内城市,国外只能到国家名称,免费版的自己定期更新Ip数据库即可。
以下为C#调用代码
class Program
{
static void Main(string[] args)
{
try
{
//传入要查询的ip 和17monipdb.datx 下载的ip库所在位置
string address = IPAddress.FindIPAddress("1.198.219.205", @"D:\开发资料\资料\Code\IP地址获取\IPAddress\IPAddress\bin\Debug");
Console.WriteLine(address);
}
catch (IOException ioex)
{
Console.WriteLine(ioex.StackTrace);
}
catch (IPv4FormatException ipex)
{
Console.WriteLine(ipex.StackTrace);
}
Console.ReadKey(true);
}
}
public class IPAddress
{
public static string FindIPAddress(string ip,string basePath)
{
string address = "";
if (string.IsNullOrEmpty(ip))
{
return address;
}
try
{
string path = string.Format("{0}\\{1}", basePath, "17monipdb.datx");
City city = new City(path);
string[] arr = city.find(ip);
if (arr != null && arr.Length > 0)
{
address = string.Join("-", arr);
}
}
catch (IOException ioex)
{
Console.WriteLine(ioex.StackTrace);
}
catch (IPv4FormatException ipex)
{
Console.WriteLine(ipex.StackTrace);
}
return address.TrimEnd("-".ToCharArray());
}
}
IPIP.net官网在这
如果您觉得我的文章对您有所帮助,欢迎扫码进行赞赏!