25

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 ?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Yazginin Firati
  • 321
  • 1
  • 4
  • 6

5 Answers5

31

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"
}
dtb
  • 213,145
  • 36
  • 401
  • 431
17

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

mike james
  • 8,840
  • 3
  • 18
  • 21
  • It will not work, `new Uri("http://www.google.com").GetLeftPart(UriPartial.Authority).Replace("http://", "") ≡ "www.google.com"` – Mekanik Apr 30 '14 at 08:37
  • 1
    See updated. Thanks for noticing. – mike james Apr 30 '14 at 12:05
  • 1
    For a different scheme, you can use like this: Uri uri = new Uri(url); string domain = uri.GetLeftPart(UriPartial.Authority).Replace("/www.", "/").Replace(uri.GetLeftPart(UriPartial.Scheme), ""); – danpop Mar 14 '16 at 13:26
7

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. :/

Craig Celeste
  • 12,207
  • 10
  • 42
  • 49
3

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 
MartyIX
  • 27,828
  • 29
  • 136
  • 207
1

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

Mekanik
  • 2,532
  • 1
  • 22
  • 20