此發行版為 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