使用 IP2Location 在 .NET 中查询地理位置和代理数据
本文关键字:地理位置 代理 数据 查询 IP2Location NET 使用 | 更新日期: 2023-09-18 11:27:41
在本教程中,我们将展示如何使用IP2Location库输入IP地址并取回IP地理位置和代理数据。开发人员可以使用地理位置信息来满足业务需求,例如页面重定向和欺诈预防。
先决条件( Pre-requisites )
- Microsoft Visual Studio 来编译代码。
- Microsoft .NET Framework 4.61 或更高版本。
- IP2位置 LITE DB11 BIN 数据库。
- IP2代理LITE PX8 BIN数据库。
我们假设您已经有一个能够处理.NET Framework 4.61或更高版本的Microsoft Visual Studio版本。
开始( Getting started )
首先,您需要下载IP2Location和IP2Proxy数据的免费IP地理位置BIN数据库。这两个数据库都可以免费使用,但需要署名。
下载免费的 IP2Location LITE DB11 数据:
https://lite.ip2location.com/ip2location-lite
下载免费的 IP2Proxy LITE PX8 数据:
https://lite.ip2location.com/ip2proxy-lite
下载上面的两个压缩文件后,您需要提取它们各自的 BIN 文件并将它们复制到某个地方的文件夹中,例如我们的示例中为 C:\myfolder\。
在 Visual Studio 项目中,转到 NuGet 包管理器并安装以下 2 个 NuGet 包:
https://www.nuget.org/packages/IP2Location.IPGeolocation/
https://www.nuget.org/packages/IP2Proxy/
如何查询这两个组件( How to query both components )
我们将只展示如何创建和调用组件。
首先,创建下面的 QueryIP2Location 函数将接受一个 IP 地址并输出地理位置结果。
private string QueryIP2Location(string strIPAddress)
{
IP2Location.IPResult oIPResult = new IP2Location.IPResult();
IP2Location.Component oIP2Location = new IP2Location.Component();
String result = String.Empty;
try
{
if (strIPAddress != "")
{
oIP2Location.IPDatabasePath = @"C:\myfolder\IP2LOCATION-LITE-DB11.BIN";
oIPResult = oIP2Location.IPQuery(strIPAddress);
switch (oIPResult.Status.ToString())
{
case "OK":
result += "IP2Location GeoLocation Results:\n===========================================\n";
result += "IP Address: " + oIPResult.IPAddress + "\n";
result += "Country Code: " + oIPResult.CountryShort + "\n";
result += "Country Name: " + oIPResult.CountryLong + "\n";
result += "Region: " + oIPResult.Region + "\n";
result += "City: " + oIPResult.City + "\n";
result += "Latitude: " + oIPResult.Latitude + "\n";
result += "Longitude: " + oIPResult.Longitude + "\n";
result += "Postal Code: " + oIPResult.ZipCode + "\n";
result += "ISP Name: " + oIPResult.InternetServiceProvider + "\n";
result += "Domain Name: " + oIPResult.DomainName + "\n";
result += "Time Zone: " + oIPResult.TimeZone + "\n";
result += "Net Speed: " + oIPResult.NetSpeed + "\n";
result += "IDD Code: " + oIPResult.IDDCode + "\n";
result += "Area Code: " + oIPResult.AreaCode + "\n";
result += "Weather Station Code: " + oIPResult.WeatherStationCode + "\n";
result += "Weather Station Name: " + oIPResult.WeatherStationName + "\n";
result += "MCC: " + oIPResult.MCC + "\n";
result += "MNC: " + oIPResult.MNC + "\n";
result += "Mobile Brand: " + oIPResult.MobileBrand + "\n";
result += "Elevation: " + oIPResult.Elevation + "\n";
result += "Usage Type: " + oIPResult.UsageType + "\n";
break;
case "EMPTY_IP_ADDRESS":
result += "IP Address cannot be blank.";
break;
case "INVALID_IP_ADDRESS":
result += "Invalid IP Address.";
break;
case "MISSING_FILE":
result += "Invalid Database Path.";
break;
}
}
else
{
result += "IP Address cannot be blank.";
}
}
catch (Exception ex)
{
result += ex.Message;
}
finally
{
oIPResult = null;
oIP2Location = null;
}
return result;
}
接下来,我们将创建以下 QueryIP2Proxy
函数,该函数也采用 IP 地址并输出代理信息。
private void QueryIP2Proxy(string strIPAddress)
{
IP2Proxy.Component proxy = new IP2Proxy.Component();
IP2Proxy.ProxyResult all;
String result = String.Empty;
if(proxy.Open(@"C:\myfolder\IP2PROXY-LITE-PX8.BIN", IP2Proxy.Component.IOModes.IP2PROXY_FILE_IO) == 0)
{
all = proxy.GetAll(strIPAddress);
result += "\n\nIP2Proxy Proxy Results:\n===========================================\n";
result += "Is_Proxy: " + all.Is_Proxy.ToString() + "\n";
result += "Proxy_Type: " + all.Proxy_Type + "\n";
result += "Country_Short: " + all.Country_Short + "\n";
result += "Country_Long: " + all.Country_Long + "\n";
result += "Region: " + all.Region + "\n";
result += "City: " + all.City + "\n";
result += "ISP: " + all.ISP + "\n";
result += "Domain: " + all.Domain + "\n";
result += "Usage_Type: " + all.Usage_Type + "\n";
result += "ASN: " + all.ASN + "\n";
result += "AS: " + all.AS + "\n";
result += "Last_Seen: " + all.Last_Seen + "\n";
proxy.Close();
}
else
{
result += "Error reading BIN file.";
}
return result;
}
最后,我们只需调用这两个函数即可获取所需的信息。
QueryIP2Location("8.8.8.8");
QueryIP2Proxy("8.8.8.8");
在您的代码中实现 IP 地理定位和代理检测功能非常容易。
访问blog.ip2location.com以了解更多信息。
本文内容总结: