Distribusi ini menyediakan API untuk layanan web dan database GeoIP2 dan GeoLite2.
Kami merekomendasikan menginstal perpustakaan ini dengan NuGet. Untuk melakukannya, ketikkan yang berikut ini ke dalam Konsol Manajer Paket Visual Studio:
install-package MaxMind.GeoIP2
Geolokasi IP pada dasarnya tidak tepat. Lokasi sering kali berada di dekat pusat populasi. Lokasi apa pun yang disediakan oleh database GeoIP2 atau layanan web tidak boleh digunakan untuk mengidentifikasi alamat atau rumah tangga tertentu.
Untuk menggunakan API layanan web, pertama-tama buat objek WebServiceClient
baru dengan ID akun dan kunci lisensi Anda:
var client = new WebServiceClient(42, "license_key1");
Untuk menanyakan layanan web GeoLite2, Anda harus menyetel host ke geolite.info
:
var client = new WebServiceClient(42, "license_key1", host: "geolite.info");
Untuk menanyakan layanan web Sandbox GeoIP2, Anda harus menyetel host ke sandbox.maxmind.com
:
var client = new WebServiceClient(42, "license_key1", host: "sandbox.maxmind.com");
Anda juga dapat menentukan lokasi cadangan, host, atau batas waktu sebagai parameter opsional. Lihat dokumen API untuk informasi lebih lanjut.
Objek ini aman untuk dibagikan ke seluruh thread. Jika Anda membuat beberapa permintaan, objek tersebut harus digunakan kembali sehingga koneksi baru tidak dibuat untuk setiap permintaan. Setelah Anda selesai membuat permintaan, Anda harus membuang objek tersebut untuk memastikan koneksi ditutup dan sumber daya apa pun segera dikembalikan ke sistem.
Anda kemudian dapat memanggil metode sinkronisasi atau async yang sesuai dengan titik akhir tertentu, meneruskannya ke alamat IP yang ingin Anda cari atau tanpa parameter jika Anda ingin mencari perangkat saat ini.
Jika permintaan berhasil, pemanggilan metode akan mengembalikan kelas respons untuk titik akhir yang Anda panggil. Respons ini pada gilirannya berisi beberapa kelas model, yang masing-masing mewakili bagian data yang dikembalikan oleh layanan web.
Lihat dokumentasi API untuk detail selengkapnya.
Untuk menggunakan API layanan web dengan pola pabrik HttpClient sebagai klien yang diketik, Anda perlu melakukan hal berikut:
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
Anda dengan ID akun dan kunci lisensi Anda. ...
"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
di mana Anda perlu melakukan panggilan dan menggunakannya. [ 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
}
Untuk menggunakan API database, Anda harus membuat DatabaseReader
baru dengan representasi string jalur ke database GeoIP2 Anda. Anda juga dapat menentukan mode akses file. Anda kemudian dapat memanggil metode yang sesuai (misalnya, city
) untuk database Anda, dengan meneruskan alamat IP yang ingin Anda cari.
Jika pencarian berhasil, pemanggilan metode akan mengembalikan kelas respons untuk pencarian GeoIP2. Kelas ini pada gilirannya berisi beberapa kelas model, yang masing-masing mewakili bagian dari data yang dikembalikan oleh database.
Kami merekomendasikan penggunaan kembali objek DatabaseReader
daripada membuat yang baru untuk setiap pencarian. Pembuatan objek ini relatif mahal karena harus membaca metadata file.
Lihat dokumentasi API untuk detail selengkapnya.
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