Live2D 看板娘 / Demo

C# 根据链接获取一级域名和当前域名

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://www.xinhuanet.com/2018-08/02/c_1123215278.htm";
            //string url = "http://news.ifeng.com/a/20180803/59600750_0.shtml";

            Uri uri = new Uri(url);
            string topDomain = GetBaseDomain(uri.Host);
            Console.WriteLine("当前域名:" + uri.Host);
            Console.WriteLine("一级域名:" + topDomain);
            Console.ReadKey();
        }
        private static string GetBaseDomain(string host)
        {
            HashSet<string> HashSet = new HashSet<string>(".com|.co|.info|.net|.org|.me|.mobi|.us|.biz|.xxx|.ca|.co.jp|.com.cn|.net.cn|.org.cn|.mx|.tv|.ws|.ag|.com.ag|.net.ag|.org.ag|.am|.asia|.at|.be|.com.br|.net.br|.bz|.com.bz|.net.bz|.cc|.com.co|.net.co|.nom.co|.de|.es|.com.es|.nom.es|.org.es|.eu|.fm|.fr|.gs|.in|.co.in|.firm.in|.gen.in|.ind.in|.net.in|.org.in|.it|.jobs|.jp|.ms|.com.mx|.nl|.nu|.co.nz|.net.nz|.org.nz|.se|.tc|.tk|.tw|.com.tw|.idv.tw|.org.tw|.hk|.co.uk|.me.uk|.org.uk|.vg".Split('|'));
            string[] hs = host.Split(".".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            if (hs.Length > 2)
            {
                //传入的host地址至少有三段
                int p2 = host.LastIndexOf('.');                 //最后一次“.”出现的位置
                int p1 = host.Substring(0, p2).LastIndexOf('.');//倒数第二个“.”出现的位置
                string s1 = host.Substring(p1);
                if (!HashSet.Contains(s1))
                    return s1.TrimStart('.');

                //域名后缀为两段(有用“.”分隔)
                if (hs.Length > 3)
                    return host.Substring(host.Substring(0, p1).LastIndexOf('.'));
                else
                    return host.TrimStart('.');
            }
            else if (hs.Length == 2)
            {
                return host.TrimStart('.');
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
复制代码

后续会陆续更新其他资料,喜欢请关注哦!

我的博客:https://www.cnblogs.com/duhaoran

posted @   KysonDu  阅读(625)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
点击右上角即可分享
微信分享提示