该发行版为 GeoIP2 和 GeoLite2 Web 服务和数据库提供了 API。
我们建议使用 NuGet 安装此库。为此,请在 Visual Studio 包管理器控制台中键入以下内容:
install-package MaxMind.GeoIP2
IP 地理定位本质上是不精确的。地点通常靠近人口中心。 GeoIP2 数据库或网络服务提供的任何位置都不应用于识别特定地址或家庭。
要使用 Web 服务 API,首先使用您的帐户 ID 和许可证密钥创建一个新的WebServiceClient
对象:
var client = new WebServiceClient(42, "license_key1");
要查询 GeoLite2 Web 服务,您必须将主机设置为geolite.info
:
var client = new WebServiceClient(42, "license_key1", host: "geolite.info");
要查询 Sandbox GeoIP2 Web 服务,您必须将主机设置为sandbox.maxmind.com
:
var client = new WebServiceClient(42, "license_key1", host: "sandbox.maxmind.com");
您还可以指定后备区域设置、主机或超时作为可选参数。请参阅 API 文档以获取更多信息。
该对象可以安全地跨线程共享。如果您发出多个请求,则应重用该对象,以便不会为每个请求创建新连接。发出请求后,您应该处置该对象,以确保连接关闭并且所有资源都立即返回到系统。
然后,您可以调用与特定端点对应的同步或异步方法,向其传递您要查找的 IP 地址,如果您想查找当前设备,则不传递任何参数。
如果请求成功,方法调用将为您调用的端点返回响应类。该响应又包含多个模型类,每个模型类代表 Web 服务返回的部分数据。
有关更多详细信息,请参阅 API 文档。
要将 Web 服务 API 与 HttpClient 工厂模式一起用作类型化客户端,您需要执行以下操作:
Startup.cs
ConfigureServices
方法: // Configure to read configuration options from MaxMind section
services . Configure < WebServiceClientOptions > ( Configuration . GetSection ( " MaxMind " ) ) ;
// Configure dependency injection for WebServiceClient
services . AddHttpClient < WebServiceClient > ( ) ;
appsettings.json
中添加配置。 ...
"MaxMind" : {
"AccountId" : 123456 ,
"LicenseKey" : "1234567890" ,
// Optionally set a timeout. The default is 3000 ms.
// "Timeout": 3000,
// Optionally set host. "geolite.info" will use the GeoLite2
// web service instead of GeoIP2. "sandbox.maxmind.com" will use the
// Sandbox GeoIP2 web service instead of the production GeoIP2 web
// service.
//
// "Host": "geolite.info"
} ,
...
WebServiceClient
并使用它。 [ ApiController ]
[ Route ( " [controller] " ) ]
public class MaxMindController : ControllerBase
{
private readonly WebServiceClient _maxMindClient ;
public MaxMindController ( WebServiceClient maxMindClient )
{
_maxMindClient = maxMindClient ;
}
[ HttpGet ]
public async Task < string > Get ( )
{
var location = await _maxMindClient . CountryAsync ( ) ;
return location . Country . Name ;
}
}
// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. Set the named host argument to "geolite.info" to use the GeoLite2
// web service instead of GeoIP2. Set the named host argument to
// "sandbox.maxmind.com" to use the Sandbox GeoIP2 web service instead of
// the production GeoIP2 web service.
using ( var client = new WebServiceClient ( 42 , " license_key " ) )
{
// Do the lookup
var response = client . Country ( " 128.101.101.101 " ) ;
Console . WriteLine ( response . Country . IsoCode ) ; // 'US'
Console . WriteLine ( response . Country . Name ) ; // 'United States'
Console . WriteLine ( response . Country . Names [ " zh-CN " ] ) ; // '美国'
}
// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. Set the named host argument to "geolite.info" to use the GeoLite2
// web service instead of GeoIP2. Set the named host argument to
// "sandbox.maxmind.com" to use the Sandbox GeoIP2 web service instead of
// the production GeoIP2 web service.
using ( var client = new WebServiceClient ( 42 , " license_key " ) )
{
// Do the lookup
var response = await client . CountryAsync ( " 128.101.101.101 " ) ;
Console . WriteLine ( response . Country . IsoCode ) ; // 'US'
Console . WriteLine ( response . Country . Name ) ; // 'United States'
Console . WriteLine ( response . Country . Names [ " zh-CN " ] ) ; // '美国'
}
// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. Set the named host argument to "geolite.info" to use the GeoLite2
// web service instead of GeoIP2. Set the named host argument to
// "sandbox.maxmind.com" to use the Sandbox GeoIP2 web service instead of
// the production GeoIP2 web service.
using ( var client = new WebServiceClient ( 42 , " license_key " ) )
{
// Do the lookup
var response = client . City ( " 128.101.101.101 " ) ;
Console . WriteLine ( response . Country . IsoCode ) ; // 'US'
Console . WriteLine ( response . Country . Name ) ; // 'United States'
Console . WriteLine ( response . Country . Names [ " zh-CN " ] ) ; // '美国'
Console . WriteLine ( response . MostSpecificSubdivision . Name ) ; // 'Minnesota'
Console . WriteLine ( response . MostSpecificSubdivision . IsoCode ) ; // 'MN'
Console . WriteLine ( response . City . Name ) ; // 'Minneapolis'
Console . WriteLine ( response . Postal . Code ) ; // '55455'
Console . WriteLine ( response . Location . Latitude ) ; // 44.9733
Console . WriteLine ( response . Location . Longitude ) ; // -93.2323
}
// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. Set the named host argument to "geolite.info" to use the GeoLite2
// web service instead of GeoIP2. Set the named host argument to
// "sandbox.maxmind.com" to use the Sandbox GeoIP2 web service instead of
// the production GeoIP2 web service.
using ( var client = new WebServiceClient ( 42 , " license_key " ) )
{
// Do the lookup
var response = await client . CityAsync ( " 128.101.101.101 " ) ;
Console . WriteLine ( response . Country . IsoCode ) ; // 'US'
Console . WriteLine ( response . Country . Name ) ; // 'United States'
Console . WriteLine ( response . Country . Names [ " zh-CN " ] ) ; // '美国'
Console . WriteLine ( response . MostSpecificSubdivision . Name ) ; // 'Minnesota'
Console . WriteLine ( response . MostSpecificSubdivision . IsoCode ) ; // 'MN'
Console . WriteLine ( response . City . Name ) ; // 'Minneapolis'
Console . WriteLine ( response . Postal . Code ) ; // '55455'
Console . WriteLine ( response . Location . Latitude ) ; // 44.9733
Console . WriteLine ( response . Location . Longitude ) ; // -93.2323
}
// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. The GeoLite2 web service does not support Insights. Set the named
// host argument to "sandbox.maxmind.com" to use the Sandbox GeoIP2 web
// service instead of the production GeoIP2 web service.
using ( var client = new WebServiceClient ( 42 , " license_key " ) )
{
// Do the lookup
var response = client . Insights ( " 128.101.101.101 " ) ;
Console . WriteLine ( response . Country . IsoCode ) ; // 'US'
Console . WriteLine ( response . Country . Name ) ; // 'United States'
Console . WriteLine ( response . Country . Names [ " zh-CN " ] ) ; // '美国'
Console . WriteLine ( response . MostSpecificSubdivision . Name ) ; // 'Minnesota'
Console . WriteLine ( response . MostSpecificSubdivision . IsoCode ) ; // 'MN'
Console . WriteLine ( response . City . Name ) ; // 'Minneapolis'
Console . WriteLine ( response . Postal . Code ) ; // '55455'
Console . WriteLine ( response . Location . Latitude ) ; // 44.9733
Console . WriteLine ( response . Location . Longitude ) ; // -93.2323
}
// If you are making multiple requests, a single WebServiceClient
// should be shared across requests to allow connection reuse. The
// class is thread safe.
//
// Replace "42" with your account ID and "license_key" with your license
// key. The GeoLite2 web service does not support Insights. Set the named
// host argument to "sandbox.maxmind.com" to use the Sandbox GeoIP2 web
// service instead of the production GeoIP2 web service.
using ( var client = new WebServiceClient ( 42 , " license_key " ) )
{
// Do the lookup
var response = await client . InsightsAsync ( " 128.101.101.101 " ) ;
Console . WriteLine ( response . Country . IsoCode ) ; // 'US'
Console . WriteLine ( response . Country . Name ) ; // 'United States'
Console . WriteLine ( response . Country . Names [ " zh-CN " ] ) ; // '美国'
Console . WriteLine ( response . MostSpecificSubdivision . Name ) ; // 'Minnesota'
Console . WriteLine ( response . MostSpecificSubdivision . IsoCode ) ; // 'MN'
Console . WriteLine ( response . City . Name ) ; // 'Minneapolis'
Console . WriteLine ( response . Postal . Code ) ; // '55455'
Console . WriteLine ( response . Location . Latitude ) ; // 44.9733
Console . WriteLine ( response . Location . Longitude ) ; // -93.2323
}
要使用数据库 API,您必须创建一个新的DatabaseReader
,其中包含 GeoIP2 数据库路径的字符串表示形式。您还可以指定文件访问模式。然后,您可以为您的数据库调用适当的方法(例如city
),并将您想要查找的 IP 地址传递给它。
如果查找成功,该方法调用将返回 GeoIP2 查找的响应类。该类又包含多个模型类,每个模型类代表数据库返回的部分数据。
我们建议重用DatabaseReader
对象,而不是为每次查找创建一个新对象。该对象的创建相对昂贵,因为它必须读取文件的元数据。
有关更多详细信息,请参阅 API 文档。
using ( var reader = new DatabaseReader ( " GeoIP2-Anonymous-IP.mmdb " ) )
{
var response = reader . AnonymousIP ( " 85.25.43.84 " ) ;
Console . WriteLine ( response . IsAnonymous ) ; // true
Console . WriteLine ( response . IsAnonymousVpn ) ; // false
Console . WriteLine ( response . IsHostingProvider ) ; // false
Console . WriteLine ( response . IsPublicProxy ) ; // false
Console . WriteLine ( response . IsResidentialProxy ) ; // false
Console . WriteLine ( response . IsTorExitNode ) ; // true
Console . WriteLine ( response . IPAddress ) ; // '85.25.43.84'
}
using ( var reader = new DatabaseReader ( " GeoLite2-ASN.mmdb " ) )
{
var response = reader . Asn ( " 85.25.43.84 " ) ;
Console . WriteLine ( response . AutonomousSystemNumber ) ; // 217
Console . WriteLine ( response . AutonomousSystemOrganization ) ; // 'University of Minnesota'
Console . WriteLine ( response . IPAddress ) ; // '128.101.101.101'
}
// This creates the DatabaseReader object, which should be reused across
// lookups.
using ( var reader = new DatabaseReader ( " GeoIP2-City.mmdb " ) )
{
// Replace "City" with the appropriate method for your database, e.g.,
// "Country".
var city = reader . City ( " 128.101.101.101 " ) ;
Console . WriteLine ( city . Country . IsoCode ) ; // 'US'
Console . WriteLine ( city . Country . Name ) ; // 'United States'
Console . WriteLine ( city . Country . Names [ " zh-CN " ] ) ; // '美国'
Console . WriteLine ( city . MostSpecificSubdivision . Name ) ; // 'Minnesota'
Console . WriteLine ( city . MostSpecificSubdivision . IsoCode ) ; // 'MN'
Console . WriteLine ( city . City . Name ) ; // 'Minneapolis'
Console . WriteLine ( city . Postal . Code ) ; // '55455'
Console . WriteLine ( city . Location . Latitude ) ; // 44.9733
Console . WriteLine ( city . Location . Longitude ) ; // -93.2323
}
using ( var reader = new DatabaseReader ( " GeoIP2-Connection-Type.mmdb " ) )
{
var response = reader . ConnectionType ( " 128.101.101.101 " ) ;
Console . WriteLine ( response . ConnectionType ) ; // 'Corporate'
Console . WriteLine ( response . IPAddress ) ; // '128.101.101.101'
}
using ( var reader = new DatabaseReader ( " GeoIP2-Domain.mmdb " ) )
{
var response = reader . Domain ( " 128.101.101.101 " ) ;
Console . WriteLine ( response . Domain ) ; // 'umn.edu'
Console . WriteLine ( response . IPAddress ) ; // '128.101.101.101'
}
using ( var reader = new DatabaseReader ( " /path/to/GeoIP2-Enterprise.mmdb " ) )
{
// Use the Enterprise(ip) method to do a lookup in the Enterprise database
var response = reader . enterprise ( " 128.101.101.101 " ) ;
var country = response . Country ;
Console . WriteLine ( country . IsoCode ) ; // 'US'
Console . WriteLine ( country . Name ) ; // 'United States'
Console . WriteLine ( country . Names [ " zh-CN " ] ) ; // '美国'
Console . WriteLine ( country . Confidence ) ; // 99
var subdivision = response . MostSpecificSubdivision ;
Console . WriteLine ( subdivision . Name ) ; // 'Minnesota'
Console . WriteLine ( subdivision . IsoCode ) ; // 'MN'
Console . WriteLine ( subdivision . Confidence ) ; // 77
var city = response . City ;
Console . WriteLine ( city . Name ) ; // 'Minneapolis'
Console . WriteLine ( city . Confidence ) ; // 11
var postal = response . Postal ;
Console . WriteLine ( postal . Code ) ; // '55455'
Console . WriteLine ( postal . Confidence ) ; // 5
var location = response . Location ;
Console . WriteLine ( location . Latitude ) ; // 44.9733
Console . WriteLine ( location . Longitude ) ; // -93.2323
Console . WriteLine ( location . AccuracyRadius ) ; // 50
}
using ( var reader = new DatabaseReader ( " GeoIP2-ISP.mmdb " ) )
{
var response = reader . Isp ( " 128.101.101.101 " ) ;
Console . WriteLine ( response . AutonomousSystemNumber ) ; // 217
Console . WriteLine ( response . AutonomousSystemOrganization ) ; // 'University of Minnesota'
Console . WriteLine ( response . Isp ) ; // 'University of Minnesota'
Console . WriteLine ( response . Organization