There are numerous sample codes floating around the web that show how to do a Whois lookup, but none seem to get it quite right. So here's code that works for all TLDs, and also smoothes out some of the peculiarities of various Whois servers. It uses the Apache Commons Net library (which by itself does not implement a comprehensive lookup solution).
The general approach is:
Perform a Whois lookup against the IANA server at whois.iana.org; the result will point to the authoritative Whois server for the domain in question.
Extract the server name from the response, and then perform a lookup against that server.
This response may point to a more specific Whois server that has additional information, often run by the host's registrar. If it does, an additional lookup is performed against that server.
Additional difficulties to watch out for:
The Whois server for the ".jp" TLD returns Japanese characters, which is generally not wanted; so an option is used to turn that off.
The server handling the ".de" TLD needs an additional flag passed in, or nothing will be returned. Also, after a few lookups, this server stops returning results to a particular IP address for a while; so be careful how many requests for ".de" domains you send.
The results for various domains (like google.com, apple.com. yahoo.com, microsoft.com etc.) contain bogus subdomains that prevent proper results for the actual domain from being returned; so a special syntax is used to filter out those junk entries for the ".com" TLD.
An additional option is passed on for ".dk" domains so that more information is shown for them.