从任何 URL 获取准确的域名

发布于 2024-11-06 16:16:01 字数 336 浏览 5 评论 0原文

我需要从任何 URL 中提取准确的域名。

例如,

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

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

这在 C# 中怎么可能?是否有完整的 TLD 列表或用于该任务的解析器?

I need to extract the exact domain name from any Url.

For example,

Url : http://www.google.com --> Domain : google.com

Url : http://www.google.co.uk/path1/path2 --> Domain : google.co.uk

How can this is possible in c# ? Is there a complete TLD list or a parser for that task ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你在我安 2024-11-13 16:16:01

您可以使用 Uri 类 访问 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"
}

You can use the Uri Class to access all components of an URI:

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

var host = uri.Host;

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

However, there is no built-in way to strip the sub-domain "www" off "www.google.co.uk". You need to implement your own logic, e.g.

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"
}
別甾虛僞 2024-11-13 16:16:01

用途:

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

Use:

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://", ""));

Input:

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

Output:

stackoverflow.com

Also works for the following.

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

森末i 2024-11-13 16:16:01

尝试 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”。从那里开始进行字符串操作。 :/

Try the System.Uri class.

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

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

which returns "www.google.co.uk". From there it's string manipulation. :/

遗弃M 2024-11-13 16:16:01

使用:

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 

use:

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

Input:

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

Result:

domain = google.com 
蒲公英的约定 2024-11-13 16:16:01

另一种变体,没有依赖项:

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

Another variant, without dependencies:

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;
}

Examples:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文