Get domain name of a url in C# / .NET [duplicate]

2019-01-26 05:32发布

问题:

This question already has an answer here:

  • Top level domain from URL in C# 8 answers

The code:

string sURL = "http://subdomain.website.com/index.htm";
MessageBox.Show(new System.Uri(sURL).Host);

gives me "subdomain.website.com"

But I need the main domain "website.com" for any url or web link.

How do I do that?

回答1:

You can do this to get just the last two segments of the host name:

string[] hostParts = new System.Uri(sURL).Host.Split('.');
string domain = String.Join(".", hostParts.Skip(Math.Max(0, hostParts.Length - 2)).Take(2));

Or this:

var host = new System.Uri(sURL).Host;
var domain = host.Substring(host.LastIndexOf('.', host.LastIndexOf('.') - 1) + 1);

This method will find include at least two domain name parts, but will also include intermediate parts of two characters or less:

var host = new System.Uri(sURL).Host;
int index = host.LastIndexOf('.'), last = 3;
while (index > 0 && index >= last - 3)
{
    last = index;
    index = host.LastIndexOf('.', last - 1);
}
var domain = host.Substring(index + 1);

This will handle domains such as localhost, example.com, and example.co.uk. It's not the best method, but at least it saves you from constructing a giant list of top-level domains.



回答2:

You can try this. This can handle many kind of root domain if you define it in an array.

string sURL = "http://subdomain.website.com/index.htm";
var host = new System.Uri(sURL).Host.ToLower();

string[] col = { ".com", ".cn", ".co.uk"/*all needed domain in lower case*/ };
foreach (string name in col)
{
    if (host.EndsWith(name))
    {
        int idx = host.IndexOf(name);
        int sec = host.Substring(0, idx - 1).LastIndexOf('.');
        var rootDomain = host.Substring(sec + 1);
    }
}


回答3:

Try regular expression?

using System.Text.RegularExpressions;

string sURL = "http://subdomain.website.com/index.htm";
string sPattern = @"\w+.com";

// Instantiate the regular expression object.
Regex r = new Regex(sPattern, RegexOptions.IgnoreCase);

// Match the regular expression pattern against a text string.
Match m = r.Match(sUrl);
if (m.Success)
{
    MessageBox.Show(m.Value);
}


标签: c# .net uri c#-2.0