从C#中的URL中提取域名

时间:2010-11-08 02:01:46

标签: c# url dns extract whois

这个问题在其他语言/平台上有答案,但我在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都属于拥有xyz.com的人,我需要从URL中提取。< / p>

4 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:3)

正如@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;
    }
}

答案 2 :(得分:1)

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

答案 3 :(得分:0)

如果您需要域名,则可以在.net

中使用URi.hostadress

如果您需要内容中的网址,则需要使用正则表达式解析它们。

相关问题