这个问题在其他语言/平台上有答案,但我找不到强大的解决方案 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

当Whois中的所有者相同时,结果应该是相同的。

有帮助吗?

解决方案

我需要同样的内容,所以我写了一堂课,您可以将其复制并粘贴到解决方案中。它使用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

其他提示

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

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

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

如果您需要来自内容的URL,则需要使用Regex解析它们。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top