/// <summary> /// 获取域名的顶级域名 /// </summary> /// <param name="domain"></param> /// <returns></returns> public static string GetTopDomainName(string domain) { //https://www.safsd.asdfasdf.baidu.com.cn/ssssd/s/b/d/hhh.html?domain=sfsdf.com.cn&id=1 domain = domain.Trim().ToLower(); string rootDomain = ".com.cn|.gov.cn|.cn|.com|.net|.org|.so|.co|.mobi|.tel|.biz|.info|.name|.me|.cc|.tv|.asiz|.hk"; if (domain.StartsWith("http://")) domain = domain.Replace("http://", ""); if (domain.StartsWith("https://")) domain = domain.Replace("https://", ""); if (domain.StartsWith("www.")) domain = domain.Replace("www.", ""); //safsd.asdfasdf.baidu.com.cn/ssssd/s/b/d/hhh.html?domain=sfsdf.com.cn&id=1 if (domain.IndexOf("/") > 0) domain = domain.Substring(0, domain.IndexOf("/")); //safsd.asdfasdf.baidu.com.cn foreach (string item in rootDomain.Split('|')) { if (domain.EndsWith(item)) { domain = domain.Replace(item, ""); if (domain.LastIndexOf(".") > 0)//adfasd.asdfas.cn { domain = domain.Replace(domain.Substring(0, domain.LastIndexOf(".") + 1), ""); } return domain + item; } continue; } return ""; }