[Java] Whois Lookup in Apach common.net

Whois is a protocol to look up domain name information. It can be done in using socket connect to whois server and send request. This example will show how to use Apache common.net to get domain information in maven prject.

  1. Add Apache Common.net dependency.
    In pom.xml, add settings below in tag dependencies.

    <dependencies>
            <dependency>
                <groupId>commons-net</groupId>
                <artifactId>commons-net</artifactId>
                <version>latest</version>
            </dependency>
    </dependencies>
  2. Create method for calling whois lookup.
    In main.java, add code as below:

    import org.apache.commons.net.whois.WhoisClient;
    
    public static String whois(String domainName, String whoisServer, int timeout) {
            WhoisClient whoisClient = new WhoisClient();
            String result = "";
            try {
                whoisClient.connect(whoisServer);
                whoisClient.setDefaultTimeout(timeout);
                if(whoisClient.isAvailable() && whoisClient.isConnected()) {
                    result = whoisClient.query(domainName);
                }
                whoisClient.disconnect();
            } catch (IOException exception) {
                exception.printStackTrace();
            }
            return result;
        }

    This method will call defined whoisServer with specific timeout, after response collect and close connection.

  3. Testing
    In main.java, add code as below and run program:

    public static void main(String[] args) throws IOException {
        System.out.println(whois("google.com", "whois.iana.org", 5000));
    }

    Expected it will get result as below:

About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.