How to extract root domain in C#?
www.google.co.in => google.co.in
google.co.in => google.co.in
coo.coo.coo.coo.com => coo.com
www.google.com => google.com
Do I have to hardcode all the top-level domains into my application?
The code I have is found on every topic concerning this problem:
string domainName = host.Split('.')[host.Split('.').Count() - 2] + "." +
host.Split('.')[host.Split('.').Count() - 1];
But it doesn't work for domains like google.co.uk (as it returns co.uk)
Edit:
What I have found working so far is doing a HTTP request to "http://whois.domaintools.com/www.domain.org" which returns a 301 response code with a url containing the root domain. This is the most reliable solution for me at the moment. Maybe there is another free API for doing this?