Whois Lookup

Last edited: 28 February 2018

desktop viewHomePage

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:

Additional difficulties to watch out for:

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.

// 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 = "whois.iana.org";
    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 google.com, apple.com. yahoo.com, microsoft.com 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.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");