2

i need to retrieve an top level domain from the url means ex

http://www.google.com

this can be attained by

url.getHost();

but the thing is in case i have some sub domain with url

http://example.google.com

it extracts example.google.com i need to extract the google.com alone is there is any way to extract it...

rathna
  • 81
  • 1
  • 3
  • 1
    possible duplicate of [Get the second level domain of an URL (java)](http://stackoverflow.com/questions/1923815/get-the-second-level-domain-of-an-url-java) – Bozho Jan 27 '11 at 07:53
  • 1
    The TOP-Level domain of `example.google.com` and `google.com` is `com`. `google.com` is second level domain. The most righteous `.`-part is always the top-level, followed by second-level and so on. – Michael Konietzka Jan 27 '11 at 08:26
  • be careful, there are other url schemes in use; for example, `google.co.uk` – PA. Jan 27 '11 at 09:44
  • yes i am aware of it ..the thing is i need to extract the exact domain name – rathna Jan 27 '11 at 10:07

2 Answers2

6

With google guava (uses public suffixes)

final InternetDomainName topPrivateDomain = InternetDomainName.from(uriHost).topPrivateDomain();
topPrivateDomain.name();
user85155
  • 1,370
  • 16
  • 24
0

Try the java library at http://sourceforge.net/projects/publicsuffix/
That might help since you need all known domain suffixes and I think that jar just do that.

Haim Sulam
  • 316
  • 1
  • 5