Requirements: Nowadays, the comment area of many websites can display the author's attribution address, that is, the IP attribution address of the author's evaluation, and there are generally two ways to query the address through IP, one is to regularly update the IP address library locally, and the other is to adjust some API interfaces (free or paid).
I wrote before that I used GeoLite2 to query IP addresses offline, and now it seems that it has been charged, as follows:
Of course, there are also some free IP libraries (Pure free IP library:The hyperlink login is visible.)
This paper uses the ip2region database to query addresses through IP, ip2region v2.0 - is an offline IP address location database and IP location data management framework, with a query efficiency of 10 microseconds, and provides XDB data generation and query client implementations for many mainstream programming languages.
GitHub address:The hyperlink login is visible.
The region information for each IP data segment is in a fixed format:Country|region|province|city| ISPOnly most of China's data is subdivided into cities, and the data of other countries can only be located to countries, and the options in the back and front are all 0.
First, download "ip2region.xdb"File, address:The hyperlink login is visible.
Create a new .NET 6 console application, use nuget to introduce a library written by others, and the command is as follows:
The code is as follows:
China|0|Shanghai|Shanghai|Mobile If you are a ASP.NET Core website, you can register for the service using the following code:
There are three configurations of CachePolicy in the constructor, as follows:
namespace IP2Region.Net.XDB;
public enum CachePolicy
{ /// <summary> /// no cache , not thread safe! /// </summary> File, /// <summary> /// cache vector index , reduce the number of IO operations , not thread safe! /// </summary> VectorIndex, /// <summary> /// default cache policy , cache whole xdb file , thread safe /// </summary> Content
} (End)
|