QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

查看: 4185|回复: 0

[C#/.NET] 基于ip2region的离线IP定位库

[复制链接]

等级头衔

积分成就    金币 : 2861
   泡泡 : 1516
   精华 : 6
   在线时间 : 1328 小时
   最后登录 : 2026-7-17

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老活跃会员

联系方式
发表于 2022-6-20 09:17:05 | 显示全部楼层 |阅读模式
一、前言
; \/ U2 N* U5 p  T       在开发中,我们需要记录关于登录者的ip和位置信息,可以通过ip2region来实现!比如说,qq,gitee等 我们在异地登录时,会提醒我们账号异地登录!外卖等软件,需要地理位置,如果我们的gps关闭,也可以根据IP分析地理位置等。( q3 o" r/ {3 i; u
二、简介
% l$ Y: E. d+ [% t8 l! V( U0 d       ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java、php、c、python、nodejs、golang、c#等查询绑定和Binary、B树、内存三种查询算法。每条ip数据段都固定了格式:5 p7 B/ b( a4 e6 h
       城市Id|国家|区域|省份|城市|ISP_
& w( L$ Z2 Z9 S4 E6 Igithub地址:https://github.com/lionsoul2014/ip2region$ s& N6 K& t2 M. N
三、使用
, N) b) x3 W7 a  c! ?( Vnuget安装7 D. e$ y) V  d+ f
  • Install-Package IP2Region
    7 \; D1 Q' `; ^: ?2 s
下载ip2region.db. M, x8 @. m. \/ g, K& W) ~* N
    然后找到data/文件夹下面找到ip2region.db复制到项目resources下,使用就很简单了,代码就一行。  X$ K( c7 O/ U( b1 j7 ^
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();
  }
}
四、总结* t: F' _( q( ?
       文章来源于即兴发挥,虽然我工作中现在还没遇到这种需求,但是了解一下总归是有好处的,欢迎大家留言讨论!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号|渝公网安备50010502503914号 )

GMT+8, 2026-8-11 00:48

Powered by paopaomj X3.5 © 2016-2025 sitemap

快速回复 返回顶部 返回列表