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.
- 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>
- 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.
- 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:
Leave a Reply