How do I query a Whois server using a socket from names in a text file?
Author: Deron Eriksson
Description: This Java tutorial describes how to query a Whois server using domain names in a text file.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


The MultiWhois class shown here uses bits and pieces of other tutorials to read a text file of domain names, perform WhoisW queries on those domain names, and output the results to an HTMLW file. This class uses the whois.enom.com Whois server. Whois servers can send back results in many different formats, so the code that determines if a domain name 'Exists' or 'Does Not Exist' is not robust. This code is not robust in many other ways too. For example, if the server returns a 'Malformed Request' message, the MultiWhois class reports that the name exists even though this is an error condition. However, MultiWhois does serve to illustrate some useful concepts.

This code should not be used to bombard a Whois server with an unreasonable number of requests in a short period of time. When querying a list of about 300 domain names, I received a message after about 150 saying that I'd need to wait 30 minutes before resuming, since I'd exceeded a query quota. In addition, in order to try to be nice, I added a Thread.sleep(3000) after each query in order to throttle the requests to the Whois server.

The MultiWhois class reads a list of domain names in from the "input.txt" file (one name per line) into a List. It loops over this list of domain names, performing a Whois query for each name via a socket connection to the Whois server. It generates output in HTML tabular format and writes this to the "results.htm" file.

MultiWhois.java

package test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class MultiWhois {

	public static final String WHOIS_SERVER = "whois.enom.com";

	public static void main(String[] args) throws Exception {

		String inputFile = "input.txt";
		String outputFile = "results.htm";

		FileWriter fileWriter = new FileWriter(outputFile);
		PrintWriter out = new PrintWriter(fileWriter);
		out.println("<table border=1 cellpadding=2 cellspacing=0>");
		out.println("<tr><th>Name</th><th>Status</th><th>output</th></tr>");

		List<String> nameList = getNamesFromFile(inputFile);
		for (String domainNameToCheck : nameList) {
			performQueryAndOutputResultToWriter(out, domainNameToCheck);
			Thread.sleep(3000); // let's put in a pause so we don't bombard the whois server
		}

		out.println("</table>");
		out.flush();
		out.close();
		fileWriter.close();
	}

	public static String performWhoisQuery(String host, int port, String query) throws Exception {
		System.out.println("**** Performing whois query for '" + query + "' at " + host + ":" + port);

		Socket socket = new Socket(host, port);

		InputStreamReader isr = new InputStreamReader(socket.getInputStream());
		BufferedReader in = new BufferedReader(isr);

		PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
		out.println(query);

		String line = "";
		StringBuffer sb = new StringBuffer();
		while ((line = in.readLine()) != null) {
			System.out.println(line);
			sb.append(line);
			sb.append("<br/>\n");
		}
		return sb.toString();
	}

	public static void performQueryAndOutputResultToWriter(PrintWriter out, String domainNameToCheck) throws Exception {
		String queryResult = performWhoisQuery(WHOIS_SERVER, 43, domainNameToCheck);
		boolean found = true;
		if (queryResult.indexOf("FW-1") != -1) {
			found = false;
		}

		out.println("<tr><td>");
		out.println(domainNameToCheck);
		out.println("</td><td>");
		if (found) {
			out.println("Exists");
		} else {
			out.println("Does Not Exist");
		}
		out.println("</td><td>");
		out.println(queryResult);
		out.println("</td></tr>");
	}

	public static List<String> getNamesFromFile(String inputFile) throws Exception {
		FileReader fileReader = new FileReader(inputFile);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		String inputLine;
		List<String> lineList = new ArrayList<String>();
		while ((inputLine = bufferedReader.readLine()) != null) {
			lineList.add(inputLine);
		}
		fileReader.close();
		return lineList;
	}

}

I tested MultiWhois with the following input file:

input.txt

imaginarydomainname.com
abcnews.com
54321squirrel.com

Executing MultiWhois resulted in the generation of the following results.htm file given the input file listed above.

results.htm

NameStatusoutput
imaginarydomainname.com Does Not Exist Domain not found. Code FW-1: Whois data not currently available for imaginarydomainname.com
abcnews.com Exists Visit AboutUs.org for more information about ABCNEWS.COM
AboutUs: ABCNEWS.COM




Registrant:
ABC, Inc.
77 West 66th Street
506 Second Ave. Suite 2100
New York, NY 10023-6298
US

Domain Name: ABCNEWS.COM

------------------------------------------------------------------------
Promote your business to millions of viewers for only $1 a month
Learn how you can get an Enhanced Business Listing here for your domain name.
Learn more at http://www.NetworkSolutions.com/
------------------------------------------------------------------------

Administrative Contact, Technical Contact:
dig.com dns-ops@DIG.COM
506 2ND AVE STE 2100
SEATTLE, WA 98104-2300
US
(206) 664-4000 fax: (206) 664-4009


Record expires on 21-Apr-2008.
Record created on 20-Apr-1995.
Database last updated on 21-Jan-2008 18:15:27 EST.

Domain servers in listed order:

SENS01.DIG.COM 199.181.134.16
SENS02.DIG.COM 199.181.135.199
ORNS01.DIG.COM 198.187.189.44
ORNS02.DIG.COM 198.187.190.44
54321squirrel.com Does Not Exist Domain not found. Code FW-1: Whois data not currently available for 54321squirrel.com