Showing posts with label WhoisClient. Show all posts
Showing posts with label WhoisClient. Show all posts

Wednesday, November 11, 2015

Query InterNIC server's whois

Last example show Java example using WhoisClient class to query whois. Alternatively, we can query InterNIC server's whois without WhoisClient class.


package javawhois;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaWhoIs {
    
    private final static String WHO ="google.com";

    private final static String WHOIS_HOST = "whois.internic.net";
    private final static int WHOIS_PORT = 43;

    public static void main(String[] args) {
        int c;
        Socket socket = null; 

        String query = "=" + WHO + "\r\n";
        byte buf[] = query.getBytes();
        
        try {
            socket = new Socket(WHOIS_HOST, WHOIS_PORT);
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream(); 

            out.write(buf); 
            out.flush();
            while ((c = in.read()) != -1) { 
                System.out.print((char) c); 
            } 
            System.out.print("\nDone\n");
        } catch (IOException ex) {
            Logger.getLogger(JavaWhoIs.class.getName()).log(Level.SEVERE, null, ex);
            System.out.print(ex.getMessage());
        } finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException ex) {
                    Logger.getLogger(JavaWhoIs.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
}


Sunday, November 8, 2015

Java example using WhoisClient class

The WhoisClient class implements the client side of the Internet Whois Protocol defined in RFC 954. To query a host you create a WhoisClient instance, connect to the host, query the host, and finally disconnect from the host. If the whois service you want to query is on a non-standard port, connect to the host at that port.
org.apache.commons.net.whois.WhoisClient

Example:
package javawhoisclient;

import java.io.IOException;
import org.apache.commons.net.whois.WhoisClient;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaWhoisClient {

    public static void main(String[] args) {
        WhoisClient whois;

        whois = new WhoisClient();

        try {
            whois.connect(WhoisClient.DEFAULT_HOST);
            System.out.println(whois.query("=google.com"));
            whois.disconnect();
        } catch (IOException e) {
            System.err.println("Error I/O exception: " + e.getMessage());
            return;
        }
    }

}




To use WhoisClient class in your program, you have to include library commons-net-3.3 in your code, it can be download here: https://commons.apache.org/proper/commons-net/index.html

This video show how to download and add library commons-net-3.3 to NetBeans IDE.


Next:
query InterNIC server's whois without WhoisClient class.