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...

4
  • 1
    possible duplicate of Get the second level domain of an URL (java) Commented Jan 27, 2011 at 7: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. Commented Jan 27, 2011 at 8:26
  • be careful, there are other url schemes in use; for example, google.co.uk Commented Jan 27, 2011 at 9:44
  • yes i am aware of it ..the thing is i need to extract the exact domain name Commented Jan 27, 2011 at 10:07

2 Answers 2

6

With google guava (uses public suffixes)

final InternetDomainName topPrivateDomain = InternetDomainName.from(uriHost).topPrivateDomain();
topPrivateDomain.name();
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.