【问题标题】:Extract domain name from URL in C#从 C# 中的 URL 中提取域名
【发布时间】:2011-05-06 11:14:12
【问题描述】:

这个问题有其他语言/平台的答案,但我在C# 中找不到可靠的解决方案。在这里,我正在寻找我们在WHOIS 中使用的 URL 部分,所以我对子域、端口、架构等不感兴趣。

Example 1: http://s1.website.co.uk/folder/querystring?key=value => website.co.uk
Example 2: ftp://username:password@website.com => website.com

当 whois 中的所有者相同时,结果应该相同,因此 sub1.xyz.com 和 sub2.xyz.com 都属于谁拥有我需要从 URL 中提取的 xyz.com。

【问题讨论】:

    标签: c# url dns extract whois


    【解决方案1】:

    我也需要,所以我编写了一个类,您可以将其复制并粘贴到您的解决方案中。它使用 tld 的硬编码字符串数组。 http://pastebin.com/raw.php?i=VY3DCNhp

    Console.WriteLine(GetDomain.GetDomainFromUrl("http://www.beta.microsoft.com/path/page.htm"));
    

    输出microsoft.com

    Console.WriteLine(GetDomain.GetDomainFromUrl("http://www.beta.microsoft.co.uk/path/page.htm"));
    

    输出microsoft.co.uk

    【讨论】:

    • 感谢您分享您的工作。另一个问题是更新列表,但我认为它不会经常更改。
    • 这门课很棒。我已经从the PublicSuffix list 中删除了所有 TLD 的完整列表,今天更新。它几乎是您提交的那个的两倍(约 6390 个条目) 如果您需要它,您可以在pastebin.com/raw.php?i=PxKWw5jt 找到该变量。 :) 再一次感谢你! :)
    • 目前没有可用的链接。
    【解决方案2】:

    正如@Pete 所说,这有点复杂,但我会试一试。

    请注意,此应用程序必须包含已知 TLD 的完整列表。这些可以从http://publicsuffix.org/ 检索。留下从该站点提取的列表作为读者练习。

    class Program
    {
        static void Main(string[] args)
        {
            var testCases = new[]
            {
                "www.domain.com.ac",
                "www.domain.ac",
                "domain.com.ac",
                "domain.ac",
                "localdomain",
                "localdomain.local"
            };
    
            foreach (string testCase in testCases)
            {
                Console.WriteLine("{0} => {1}", testCase, UriHelper.GetDomainFromUri(new Uri("http://" + testCase + "/")));
            }
    
            /* Produces the following results:
    
                www.domain.com.ac => domain.com.ac
                www.domain.ac => domain.ac
                domain.com.ac => domain.com.ac
                domain.ac => domain.ac
                localdomain => localdomain
                localdomain.local => localdomain.local
             */
        }
    }
    
    public static class UriHelper
    {
        private static HashSet<string> _tlds;
    
        static UriHelper()
        {
            _tlds = new HashSet<string>
            {
                "com.ac",
                "edu.ac",
                "gov.ac",
                "net.ac",
                "mil.ac",
                "org.ac",
                "ac"
    
                // Complete this list from http://publicsuffix.org/.
            };
        }
    
        public static string GetDomainFromUri(Uri uri)
        {
            return GetDomainFromHostName(uri.Host);
        }
    
        public static string GetDomainFromHostName(string hostName)
        {
            string[] hostNameParts = hostName.Split('.');
    
            if (hostNameParts.Length == 1)
                return hostNameParts[0];
    
            int matchingParts = FindMatchingParts(hostNameParts, 1);
    
            return GetPartOfHostName(hostNameParts, hostNameParts.Length - matchingParts);
        }
    
        private static int FindMatchingParts(string[] hostNameParts, int offset)
        {
            if (offset == hostNameParts.Length)
                return hostNameParts.Length;
    
            string domain = GetPartOfHostName(hostNameParts, offset);
    
            if (_tlds.Contains(domain.ToLowerInvariant()))
                return (hostNameParts.Length - offset) + 1;
    
            return FindMatchingParts(hostNameParts, offset + 1);
        }
    
        private static string GetPartOfHostName(string[] hostNameParts, int offset)
        {
            var sb = new StringBuilder();
    
            for (int i = offset; i < hostNameParts.Length; i++)
            {
                if (sb.Length > 0)
                    sb.Append('.');
    
                sb.Append(hostNameParts[i]);
            }
    
            string domain = sb.ToString();
            return domain;
        }
    }
    

    【讨论】:

    • @Xaqron - 我不知道怎么做。我已将整个代码复制到一个新的控制台项目中,它可以正确编译并给出预期的结果。您能否更具体地说明您认为缺少什么?
    • GetDomainFromHostName() 方法下面缺少笑话,但它现在就在那里。谢谢。
    【解决方案3】:

    您可以获得的最接近的是System.Uri.Host 属性,它将提取 sub1.xyz.com 部分。不幸的是,很难知道主机的“顶级”部分到底是什么(例如 sub1.foo.co.uk 与 sub1.xyz.com)

    【讨论】:

    • 几乎不可能确定哪个是顶层,因为例如 .co.uk 需要两个部分,但 .info 或 .jp 需要的不是 .[a-zA-Z]{3}
    • Public Suffix List 可用于此类任务。但最简单的方法可能是 whois 整个主机名并一次处理一个段,直到获得结果。
    • 该列表“应该”是正确的,但这就是我的观点。 “应该”不是一个很好的商业规则......
    • @bobince 是的,这可能是做到这一点的最可靠的方法,按照自己的方式进行细分。
    【解决方案4】:

    如果你需要域名,那么你可以在 .net 中使用 URi.hostadress

    如果您需要内容中的 url,那么您需要使用正则表达式来解析它们。

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 1970-01-01
      • 2017-10-16
      • 2018-09-08
      • 2011-11-05
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      相关资源
      最近更新 更多