从任何URL获取确切的域名

时间:2011-05-12 20:55:56

标签: c# dns uri tld

我需要从任何网址中提取确切的域名。

例如,

网址:http://www.google.com - >域名:google.com

网址:http://www.google.co.uk/path1/path2 - >域名:google.co.uk

如何才能在c#中实现这一目标?是否有完整的TLD列表或该任务的解析器?

5 个答案:

答案 0 :(得分:31)

您可以使用Uri Class访问URI的所有组件:

var uri = new Uri("http://www.google.co.uk/path1/path2");

var host = uri.Host;

// host == "www.google.co.uk"

但是,没有内置方法可以将子域“www”从“www.google.co.uk”中删除。您需要实现自己的逻辑,例如

var parts = host.ToLowerInvariant().Split('.');

if (parts.Length >= 3 &&
    parts[parts.Length - 1] == "uk" &&
    parts[parts.Length - 2] == "co")
{
    var result = parts[parts.Length - 3] + ".co.uk";

    // result == "google.co.uk"
}

答案 1 :(得分:16)

使用:

new Uri("http://www.stackoverflow.com/questions/5984361/c-sharp-getting-exact-domain-name-from-any-url?s=45faab89-43eb-41dc-aa5b-8a93f2eaeb74#new-answer").GetLeftPart(UriPartial.Authority).Replace("/www.", "/").Replace("http://", ""));

输入:

http://www.stackoverflow.com/questions/5984361/c-sharp-getting-exact-domain-name-from-any-url?s=45faab89-43eb-41dc-aa5b-8a93f2eaeb74#new-answer

输出:

stackoverflow.com

也适用于以下情况。

http://www.google.com→google.com

http://www.google.co.uk/path1/path2→google.co.uk

http://localhost.intranet:88/path1/path2→localhost.intranet:88

http://www2.google.com→www2.google.com

答案 2 :(得分:7)

尝试使用System.Uri类。

http://msdn.microsoft.com/en-us/library/system.uri.aspx

new Uri("http://www.google.co.uk/path1/path2").Host

返回“www.google.co.uk”。从那里它是字符串操纵。 :/

答案 3 :(得分:3)

使用:

var uri =new Uri(Request.RawUrl); // to get the url from request or replace by your own
var domain = uri.GetLeftPart( UriPartial.Authority );

输入:

Url = http://google.com/?search=true&q=how+to+use+google

结果:

domain = google.com 

答案 4 :(得分:0)

另一种变体,没有依赖关系:

string GetDomainPart(string url)
{
    var doubleSlashesIndex = url.IndexOf("://");
    var start = doubleSlashesIndex != -1 ? doubleSlashesIndex + "://".Length : 0;
    var end = url.IndexOf("/", start);
    if (end == -1)
        end = url.Length;

    string trimmed = url.Substring(start, end - start);
    if (trimmed.StartsWith("www."))
        trimmed = trimmed.Substring("www.".Length );
    return trimmed;
}

示例:

http://www.google.com → google.com

http://www.google.co.uk/path1/path2 → google.co.uk

http://localhost.intranet:88/path1/path2 → localhost.intranet:88

http://www2.google.com → www2.google.com