This question already has an answer here:
- Get host domain from URL? 8 answers
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 ?
You can use the Uri Class to access all components of an URI:
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.
use:
Input:
Result:
Use:
Input:
Output:
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
Try the System.Uri class.
http://msdn.microsoft.com/en-us/library/system.uri.aspx
which returns "www.google.co.uk". From there it's string manipulation. :/
Another variant, without dependencies:
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