一、前言
7 Y) ]) w, w1 s1 J: h 在开发中,我们需要记录关于登录者的ip和位置信息,可以通过ip2region来实现!比如说,qq,gitee等 我们在异地登录时,会提醒我们账号异地登录!外卖等软件,需要地理位置,如果我们的gps关闭,也可以根据IP分析地理位置等。' y0 d8 x5 T* c5 R. m8 H
二、简介6 I; s/ X; l1 \1 |) Q W+ r
ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java、php、c、python、nodejs、golang、c#等查询绑定和Binary、B树、内存三种查询算法。每条ip数据段都固定了格式:1 g" {5 W. i* R% \
城市Id|国家|区域|省份|城市|ISP_
2 w1 F$ K0 ]* ^9 w. ^github地址:https://github.com/lionsoul2014/ip2region
& w$ l3 X2 ~+ L2 S7 G; P三、使用
8 j# {: Y5 X: V& E1 inuget安装9 d" w7 w/ C* |" I: M. G: }8 y! L
- Install-Package IP2Region
/ C3 L0 C: C& G9 n' @8 G, ` 下载ip2region.db. k, B* X2 @' x- h0 e
然后找到data/文件夹下面找到ip2region.db复制到项目resources下,使用就很简单了,代码就一行。3 Z" a* P: _5 W4 `) [
static 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();
}
} 四、总结% B/ e1 G! W I o+ }& G$ C# Q
文章来源于即兴发挥,虽然我工作中现在还没遇到这种需求,但是了解一下总归是有好处的,欢迎大家留言讨论! |