代码之家  ›  专栏  ›  技术社区  ›  Xaqron

从c中的url提取域名#

  •  5
  • Xaqron  · 技术社区  · 14 年前

    这个问题在其他语言/平台中有答案,但在 C# . 这里我要找的是我们使用的URL部分 WHOIS 所以我对子域、端口、模式等不感兴趣。

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

    当who is中的所有者相同时,结果应该是相同的,所以sub1.xyz.com和sub2.xyz.com都属于拥有xyz.com的人,我需要从URL中提取该xyz.com。

    4 回复  |  直到 14 年前
        1
  •  6
  •   servermanfail    14 年前

    我需要同样的方法,所以我编写了一个类,您可以复制并粘贴到您的解决方案中。它使用一个硬编码的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

        2
  •  3
  •   Pieter van Ginkel    14 年前

    正如@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;
        }
    }
    
        3
  •  1
  •   Pete    14 年前

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

        4
  •  0
  •   Steven Spielberg    14 年前

    如果需要域名,则可以在.NET中使用uri.hostadress

    如果您需要来自内容的URL,那么您需要使用regex解析它们。