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 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.
  • 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. Some of the .za subdomains are thus special-cased.

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.



import java.util.*;

import org.apache.commons.net.whois.WhoisClient;
// You don't actually need the complete Apache Commons Net library;
// the FingerClient, SocketClient and WhoisClient classes are sufficient.

public class WhoisLookup {

    public static final String IANA_WHOIS_SERVER = "whois.iana.org";
    public static final int WHOIS_PORT = 43;

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

        String whoisServer = null;

        WhoisClient whoisClient = new WhoisClient();
        if (host.endsWith("web.za")) {
            whoisServer = "web-whois.registry.net.za";
        } else if (host.endsWith("org.za")) {
            whoisServer = "org-whois.registry.net.za";
        } else if (host.endsWith("net.za")) {
            whoisServer = "net-whois.registry.net.za";
        } else if (host.endsWith("co.za")) {
            whoisServer = "coza-whois.registry.net.za";
        } else {
            // get the address of the authoritative Whois server from IANA
            whoisClient.connect(IANA_WHOIS_SERVER, WHOIS_PORT);
            String results = whoisClient.query(host);
            whoisClient.disconnect();

            int idx = results.indexOf("whois:");
            if (idx < 0) {
                System.out.println("No WHOIS server available for "+host);
                System.exit(0);
            } else {
                results = results.substring(idx+6).trim();
                whoisServer = results.substring(0, results.indexOf("\n"));
            }
        }

        String tld = host.substring(host.lastIndexOf(".")+1).trim().toLowerCase();

        String query = host;
        switch (tld) {
            case "com":
                query = "domain " + host;
                break;
            case "de":
                query = "-T dn " + host;
                break;
            case "dk":
                query = "--show-handles " + host;
                break;
            case "jp":
                query = host + "/e";
                break;
            default:
        }

        // get the actual Whois data
        whoisClient.connect(whoisServer, WHOIS_PORT);
        String results = whoisClient.query(query);
        whoisClient.disconnect();
        printResults(whoisServer, results);

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

    private static void printResults (String server, String results) {
        // remove Windows-style line endings
        results = results.replaceAll("\r""");

        boolean seenDashes = false;
        boolean omit = false;
        String[] lines = results.split("\n");
        List<String> newLines = new ArrayList<>();
        for (String line : lines) {
            String trim = line.trim();
            switch (server) {
                case "whois.denic.de":
                case "whois.tcinet.ru":
                case "whois.nic.fr":
                    if (! trim.startsWith("%"))
                        newLines.add(line);
                    break;
                case "whois.dk-hostmaster.dk":
                case "whois.arin.net":
                    if (! trim.startsWith("#"))
                        newLines.add(line);
                    break;
                case "whois.nic.uk":
                    if (trim.startsWith("--"))
                        omit = true;
                    if (! omit)
                        newLines.add(line);
                    break;
                case "whois.jprs.jp":
                    if (! trim.startsWith("[ "))
                        newLines.add(line);
                    break;
                case "whois.educause.edu":
                    if (seenDashes)
                        newLines.add(line);
                    if (trim.startsWith("------"))
                        seenDashes = true;
                    break;
                default:
                    if (! omit)
                        newLines.add(line);
                    if (trim.startsWith(">>>"))
                        omit = true;
                    break;
            }
        }

        System.out.println("\n\nFrom "+server+":\n");
        for (String line : newLines) {
            System.out.println(line);
        }
    }
}


CodeSnippets