一、前言) H) p5 t: R1 Y* K4 x: \" j/ ^
在开发中,我们需要记录关于登录者的ip和位置信息,可以通过ip2region来实现!比如说,qq,gitee等 我们在异地登录时,会提醒我们账号异地登录!外卖等软件,需要地理位置,如果我们的gps关闭,也可以根据IP分析地理位置等。
' h7 \( c& ~/ X% j' s% a+ B二、简介. W7 f3 I; F* S1 G# u
ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java、php、c、python、nodejs、golang、c#等查询绑定和Binary、B树、内存三种查询算法。每条ip数据段都固定了格式:& S6 B5 Q. I6 b) }# P2 h
城市Id|国家|区域|省份|城市|ISP_
" J b- u. b d8 a0 K( E6 L5 Kgithub地址:https://github.com/lionsoul2014/ip2region
" N* n' X8 u2 @$ W4 F三、使用0 M3 c# d3 O7 j7 Q# T1 t4 n
nuget安装! c- S% U# F; m# p1 s4 M
- Install-Package IP2Region
, `7 [) {5 i5 Z) p/ j0 H. p 下载ip2region.db0 s# f) B6 m! R
然后找到data/文件夹下面找到ip2region.db复制到项目resources下,使用就很简单了,代码就一行。
/ S+ G$ z, C- W0 E! U# _0 D. Zstatic void Main(string[] args)
{
using (var _search = new DbSearcher(Environment.CurrentDirectory + @"\DB\ip2region.db"))
{
Console.WriteLine(_search.MemorySearch("183.129.193.166").Region);
Console.Read();
}
} 四、总结
" R( Y. ~: i9 a 文章来源于即兴发挥,虽然我工作中现在还没遇到这种需求,但是了解一下总归是有好处的,欢迎大家留言讨论! |