이 배포판은 GeoIP2 및 GeoLite2 웹 서비스와 데이터베이스를 위한 API를 제공합니다.
NuGet과 함께 이 라이브러리를 설치하는 것이 좋습니다. 이렇게 하려면 Visual Studio 패키지 관리자 콘솔에 다음을 입력하세요.
install-package MaxMind.GeoIP2
IP 지리적 위치는 본질적으로 부정확합니다. 위치는 인구의 중심 근처에 있는 경우가 많습니다. GeoIP2 데이터베이스 또는 웹 서비스에서 제공하는 모든 위치는 특정 주소나 가구를 식별하는 데 사용되어서는 안 됩니다.
웹 서비스 API를 사용하려면 먼저 계정 ID와 라이센스 키를 사용하여 새 WebServiceClient
개체를 만듭니다.
var client = new WebServiceClient(42, "license_key1");
GeoLite2 웹 서비스를 쿼리하려면 호스트를 geolite.info
로 설정해야 합니다.
var client = new WebServiceClient(42, "license_key1", host: "geolite.info");
Sandbox GeoIP2 웹 서비스를 쿼리하려면 호스트를 sandbox.maxmind.com
으로 설정해야 합니다.
var client = new WebServiceClient(42, "license_key1", host: "sandbox.maxmind.com");
대체 로케일, 호스트 또는 시간 초과를 선택적 매개변수로 지정할 수도 있습니다. 자세한 내용은 API 문서를 참조하세요.
이 개체는 스레드 간에 공유해도 안전합니다. 여러 요청을 하는 경우 각 요청에 대해 새 연결이 생성되지 않도록 개체를 재사용해야 합니다. 요청을 마친 후에는 연결이 닫히고 모든 리소스가 시스템에 즉시 반환되도록 개체를 삭제해야 합니다.
그런 다음 특정 끝점에 해당하는 sync 또는 async 메서드를 호출하여 조회하려는 IP 주소를 전달하거나 현재 장치를 조회하려는 경우 매개변수 없이 전달할 수 있습니다.
요청이 성공하면 메서드 호출은 호출한 엔드포인트에 대한 응답 클래스를 반환합니다. 이 응답에는 여러 모델 클래스가 포함되어 있으며 각 클래스는 웹 서비스에서 반환된 데이터의 일부를 나타냅니다.
자세한 내용은 API 문서를 참조하세요.
HttpClient 팩토리 패턴이 포함된 웹 서비스 API를 형식화된 클라이언트로 사용하려면 다음을 수행해야 합니다.
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를 사용하려면 GeoIP2 데이터베이스 경로의 문자열 표현을 사용하여 새 DatabaseReader
생성해야 합니다. 파일 액세스 모드를 지정할 수도 있습니다. 그런 다음 데이터베이스에 적합한 메소드(예: 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