分类

链接

2024 年 9 月
 1
2345678
9101112131415
16171819202122
23242526272829
30  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
.NET8实时更新nginx ip地址归属地
.NET 暂无评论 阅读(45)

简介

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安装

下载ip2region.db

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;
    }
}

 

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:.NET8实时更新nginx ip地址归属地 | Bruce's Blog

发表评论

留言无头像?