.NET8实时更新nginx ip地址归属地
简介
ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。每条ip数据段都固定了格式:
- 城市Id|国家|区域|省份|城市|ISP_
github地址:https://github.com/lionsoul2014/ip2region
使用
nuget安装
- Install-Package IP2Region
git clone https:<span class="hljs-comment">//github.com/lionsoul2014/ip2region.git</span>
然后找到data/文件夹下面找到ip2region.db复制到项目resources下
使用就很简单了,参考IP2Region方法:
public class IPUtil { // 假设你的ip2region.xdb文件路径是"D:\ip2region.xdb" static string dbPath = AppContext.BaseDirectory + @"ip2region.xdb"; // 创建Searcher实例 static ISearcher searcher = new Searcher(new CachePolicy(), dbPath); public static string IP2Region(string ipAddress) { if (String.IsNullOrEmpty(ipAddress)) { return null; } // 执行查询 var result = searcher.Search(ipAddress); if (result != null) { // 处理查询结果 Console.WriteLine($"IP: {ipAddress}"); Console.WriteLine($"Region: {result}"); result = result.Replace("|", " ").Replace("0", ""); } else { Console.WriteLine("未找到IP地址信息。"); } return result; } /// <summary> /// 根据Ip获取我们所要的信息 /// </summary> /// <param name="strIp"></param> /// <returns></returns> public static string GetIpAddress(string strIp) { string html = GetHtml("https://www.ip138.com/iplookup.asp?ip=" + strIp + "&action=2", "gb2312"); string pre = "var ip_result = {\"ASN归属地\":\""; int pos = html.IndexOf(pre); html = html.Substring(pos + pre.Length); html = html.Substring(0, html.IndexOf('"')); string[] res = html.Split(new char[] { '省', '市', ' ' }, StringSplitOptions.RemoveEmptyEntries); return string.Join(" ", res); } public static string GetHtml(string url, string encoding) { string pagehtml = string.Empty; try { using (WebClient MyWebClient = new WebClient()) { //控制台应用和.net core 需要这一句,需要安装NetGet包System.Text.Encoding.CodePages Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); Encoding encode = Encoding.GetEncoding(encoding); MyWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36"); MyWebClient.Credentials = CredentialCache.DefaultCredentials; Byte[] pageData = MyWebClient.DownloadData(url); pagehtml = encode.GetString(pageData); } } catch (Exception ex) { return "访问" + url + "失败,请检查网络配置"; } return pagehtml; } }
============ 欢迎各位老板打赏~ ===========
【上篇】小程序开发:开启数字化转型的钥匙
【下篇】The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
【下篇】The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
与本文相关的文章
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · .NET8 Mysql SSL error
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · 解决.NET Blazor子组件不刷新问题
- · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
- · .NETCORE 依赖注入服务生命周期
- · asp.net zero改mysql
- · .NET5面试汇总
- · .Net连接Mysql数据库的Convert Zero Datetime日期问题
- · vue使用element-ui中的Message 、MessageBox 、Notification
- · Asp.Net Core Filter 深入浅出的那些事-AOP
- · docker单节点启动consul