JavaRanch Home    
 
This page:         last edited 26 March 2013         What's Changed?         Edit

Whois Lookup   

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:

  1. 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.
  2. Extract the server name from the response, and then perform a lookup against that server.
  3. 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.


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);
        whoisClient.disconnect();
        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
            tmpStr = whoisClient.query(host);
        whoisClient.disconnect();
        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"));
            whoisClient.connect(actualServer, WHOIS_PORT);
            tmpStr = whoisClient.query(host);
            whoisClient.disconnect();
            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");
        System.out.println(results);
    }
}


CodeBarn CategoryCodeSamples

JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton