本文介绍Java实现简单whois查询的方法。
利用Java socket 实现根据IP地址查询该IP相关的注册信息。
相关的两个类WhoisBean.java 和WhoisQuery.java 代码实现如下:
Java代码
import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; /** * * @author Michael sun */ public class WhoisQuery { /** * * @param ip */ private void queryIpInfo(String ip) { Map map = new LinkedHashMap(); try { WhoisBean bean = new WhoisBean(); bean.setTimeout(0); // bean.setServer("whois.apnic.net"); bean.setServer("whois.ripe.net"); bean.queryInfoByIp(ip); List infoList = bean.getInfoList(); String value = ""; String key = ""; for (String infoTmp : infoList) { String[] arr = infoTmp.split(":[ ]*"); if (arr.length > 1) { key = arr[0]; value = arr[1]; } else { value = arr[0].trim(); } if (null == map.get(key)) { map.put(key, ""); } value = map.get(key) + value; map.put(key, value); } } catch (Exception e) { e.printStackTrace(); } for (Entry entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } /** * @param args */ public static void main(String[] args) { String ip = "129.42.58.216";// "163.1.13.189"; WhoisQuery query = new WhoisQuery(); query.queryIpInfo(ip); } } |