How do you get the second level domain from a given URL?
All the articles I have read so far assume that the domain has at least one dot. I need a way to get the top level domain for a particular URL.
Examples:
http://www.example.com -> example.com
http://sub.example.com -> example.com
http://example.com -> example.com
http://example:1000 -> example
http://localhost:1000 -> localhost
This code here is what I tried and doesn't work for all the above scenarios:
var uri = new Uri("http://www.example.com");
var host = uri.Host;
var p = host.LastIndexOf(".");
var domain = host.Substring(p + 1);
(Joe's linked question does not address the main issue for which I asked this question--it only considers domains that have at least one dot in it.)