There are numerous sample codes floating around the web that show how to do a WikiPedia: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; 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.

  • This approach doesn't work for the .za domain which doesn't list its WHOIS servers in this way. The actual WHOIS servers can be found here

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

// You don't actually need the complete Apache Commons Net library;
// the FingerClient, SocketClient and WhoisClient classes are sufficient.

// Note that the German Whois server denies service for a while after a few requests.

public class WhoisLookup {

    public static final String IANA_WHOIS_SERVER = "";
    public static final int WHOIS_PORT = 43;

    public static void main (String[] args) throws Exception {
        String host = args[0];

        // get the address of the authoritative Whois server from IANA
        WhoisClient whoisClient = new WhoisClient();
        whoisClient.connect(IANA_WHOIS_SERVER, WHOIS_PORT);
        String tmpStr = whoisClient.query(host);
        int idx = tmpStr.indexOf("whois:");
        tmpStr = tmpStr.substring(idx+6).trim();
        String actualServer = tmpStr.substring(0, tmpStr.indexOf("\n"));

        // we need to special-case some TLDs
        String tld = host.substring(host.lastIndexOf(".")+1).trim().toLowerCase();

        // suppress Japanese characters in output
        if ("jp".equals(tld))
            host += "/e";

        // get the actual Whois data
        whoisClient.connect(actualServer, WHOIS_PORT);
        // The prefix "domain " solves the problem with spurious server names
        // (like for,, etc.)
        if ("com".equals(tld))
            tmpStr = whoisClient.query("domain "+host);
        else if ("de".equals(tld))
            // The syntax for .de is slightly different.
            tmpStr = whoisClient.query("-T dn "+host);
        else if ("dk".equals(tld))
            // show more information for .dk
            tmpStr = whoisClient.query("--show-handles "+host);
            tmpStr = whoisClient.query(host);
        printResults(actualServer, tmpStr);

        // if there is a more specific server, query that one too
        idx = tmpStr.toLowerCase().indexOf("whois server:");
        if (idx != -1) {
            tmpStr = tmpStr.substring(idx+13).trim();
            actualServer = tmpStr.substring(0, tmpStr.indexOf("\n")).trim();
            whoisClient.connect(actualServer, WHOIS_PORT);
            tmpStr = whoisClient.query(host);
            printResults(actualServer, tmpStr);

    private static void printResults (String server, String results) {
        // remove Windows-style line endings
        results = results.replaceAll("\r""");
        System.out.println("\n\nFrom "+server+":\n");