whois,Java如何实现简单whois查询

利用Java socket 实现根据IP地址查询该IP相关的注册信息备案网站whois。whois,Java如何实现简单whois查询源代码直接下载:java_whois。zip相关的两个类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);}}Java代码import java。io。  BufferedReader;import java。io。InputStreamReader;import java。io。PrintStream;import java。net。Socket;import java。util。  ArrayList;import java。util。List;/*** WhoisBean* @author Michael sun*/public class WhoisBean {/*** server address*/private String server = “”;/*** port*/private int port = 43;/*** timeout/minute*/private int timeout = 0;/*** infoList*/private List infoList = new ArrayList();/*** @param ip* @throws Exception*/@SuppressWarnings(“unchecked”)public void queryInfoByIp(String ip) throws Exception {Socket theSocket = null;BufferedReader br = null;PrintStream ps = null;int qryCount = 0;while (qryCount 0 && readLine。  charAt(0) != ‘%’) {infoList。add(readLine);i ;// 默认读取100行数据if (i > 100 || readLine。startsWith(“source”)) {break;}}}   。

本文出自快速备案,转载时请注明出处及相应链接。

本文永久链接: https://www.175ku.com/13069.html