C# - Gets the second level domain from URI

By , 9/15/2008
(1 ratings)
Gets the second level domain from the given URI

Example:

Uri uri = new Uri("http://blog.jan-welker.de/2008/07/28/DieCommunityTermineImmerImBlick.aspx");
string secondLevelDomain = GetSecondLevelDomain(uri);

The result is: "jan-welker.de"
/// <summary> 
/// Gets the second level domain. 
/// </summary> 
/// <param name="uri">The URI.</param> 
/// <returns></returns> 
static string GetSecondLevelDomain(Uri uri) 
{ 
	string[] domains = uri.Host.Split(new char[] { '.' }); 
	return (domains[domains.Length - 2] + "." + domains[domains.Length - 1]); 
}

Tagged with URI, second level domain, TLD, domain.

1 Comments

By Patrick, 6/20/2011
Not working with domains like "http://www.example.co.uk".
 

Log in, to comment!