利用Java socket 实现whois信息的查询功能
【一】、第二版
主要做了以下修改
- 合并成一个简单的实现类
- 修正换行内容合并的bug
- 修正有关多个“:”解析的bug
具体代码如下:WhoisQueryHandler.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
package michael.socket; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; /** * whois查询 * @author michael * @see http://sjsky.iteye.com */ public class WhoisQueryHandler { /** * whois.apnic.net<br> * whois.ripe.net */ private String server = "whois.ripe.net"; /** * port */ private int port = 43; /** * timeout/minute */ private int timeout = 0; /** * @return the server */ public String getServer() { return server; } /** * @return the port */ public int getPort() { return port; } /** * @return the timeout */ public int getTimeout() { return timeout; } /** * @param pServer the server to set */ public void setServer(String pServer) { server = pServer; } /** * @param pPort the port to set */ public void setPort(int pPort) { port = pPort; } /** * @param pTimeout the timeout to set */ public void setTimeout(int pTimeout) { timeout = pTimeout; } /** * @param ipOrDomain * @return List */ public List<String> queryInfoListByWhois(String ipOrDomain) { Socket qrySocket = null; BufferedReader br = null; PrintStream ps = null; int qryCount = 0; List<String> infoList = new ArrayList<String>(); infoList.add("查询的对象:" + ipOrDomain); while (qryCount < 3) { qryCount++; try { qrySocket = new Socket(server, port); qrySocket.setSoTimeout(timeout * 1000); ps = new PrintStream(qrySocket.getOutputStream()); ps.println(ipOrDomain); br = new BufferedReader(new InputStreamReader(qrySocket .getInputStream())); String readLine = null; int lineCount = 0; System.out.println("Whois query start.... "); while ((readLine = br.readLine()) != null && lineCount < 100) { System.out.println("readLine [" + readLine + "]"); if ("".equals(readLine) || readLine.charAt(0) == '%') { continue; } lineCount++; infoList.add(readLine); } System.out.println("whois query info finish"); break; } catch (Exception e) { e.printStackTrace(); } finally { if (null != br) { try { br.close(); } catch (Exception e1) { } } if (null != ps) { try { ps.close(); } catch (Exception e1) { } } if (null != qrySocket) { try { qrySocket.close(); } catch (Exception e1) { } } } } return infoList; } /** * @param ipOrDomain * @return List */ public Map<String, String> queryInfoMapByWhois(String ipOrDomain) { List<String> infoList = this.queryInfoListByWhois(ipOrDomain); Map<String, String> infoMap = new LinkedHashMap<String, String>(); String key = ""; String value = ""; for (String info : infoList) { if (info.startsWith(" ") || info.indexOf(':') == -1) { value = info; } else { key = info.substring(0, info.indexOf(':')); value = info.substring(info.indexOf(':') + 1); } if (null == infoMap.get(key)) { infoMap.put(key, value); } else { infoMap.put(key, infoMap.get(key) + value); } } return infoMap; } /** * @param args */ public static void main(String[] args) { WhoisQueryHandler handler = new WhoisQueryHandler(); /** * "218.202.224.2"<br> * "180.168.130.146<br> * iteye.com<br> * AS9808 */ String ipOrDomain = "129.42.58.216"; Map<String, String> map = handler.queryInfoMapByWhois(ipOrDomain); for (Entry<String, String> entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } } |
【二】、第一版:
第一版的源代码直接下载: java_whois.zip
具体的代码实现如下:WhoisQuery.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
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<String, String> map = new LinkedHashMap<String, String>(); try { WhoisBean bean = new WhoisBean(); bean.setTimeout(0); // bean.setServer("whois.apnic.net"); bean.setServer("whois.ripe.net"); bean.queryInfoByIp(ip); List<String> 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<String, String> 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); } } |
WhoisBean.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
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<String> infoList = new ArrayList<String>(); /** * @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 < 5) { qryCount++; try { theSocket = new Socket(server, port); theSocket.setSoTimeout(timeout * 1000); ps = new PrintStream(theSocket.getOutputStream()); ps.println(ip); br = new BufferedReader(new InputStreamReader(theSocket .getInputStream())); infoList.add("ip:" + ip); String readLine = ""; int i = 0; System.out.println("Whois query read start.... "); while ((readLine = br.readLine()) != null) { System.out.println("***" + readLine); if (readLine.length() > 0 && readLine.charAt(0) != '%') { infoList.add(readLine); i++; // 默认读取100行数据 if (i > 100 || readLine.startsWith("source")) { break; } } } System.out.println("querylist size:" + infoList.size()); break; } catch (Exception e) { System.out.println("EXCEPTION : " + e); } finally { if (null != br) { br.close(); } if (null != ps) { ps.close(); } if (null != theSocket) { theSocket.close(); } } } } /** * @return the server */ public String getServer() { return server; } /** * @return the port */ public int getPort() { return port; } /** * @return the timeout */ public int getTimeout() { return timeout; } /** * @param pServer the server to set */ public void setServer(String pServer) { server = pServer; } /** * @param pPort the port to set */ public void setPort(int pPort) { port = pPort; } /** * @param pTimeout the timeout to set */ public void setTimeout(int pTimeout) { timeout = pTimeout; } /** * @return the infoList */ public List<String> getInfoList() { return infoList; } /** * @param pInfoList the infoList to set */ public void setInfoList(List<String> pInfoList) { infoList = pInfoList; } } |
运行WhoisQuery这个类就可以看到如下信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
Whois query read start.... ***% This is the RIPE Database query service. ***% The objects are in RPSL format. ***% ***% The RIPE Database is subject to Terms and Conditions. ***% See http://www.ripe.net/db/support/db-terms-conditions.pdf *** ***% Note: This output has been filtered. ***% To receive output for a database update, use the "-B" flag. *** ***% Information related to '129.0.0.0 - 129.255.255.255' *** ***inetnum: 129.0.0.0 - 129.255.255.255 ***netname: EU-ZZ-129 ***descr: Various Registries ***country: EU # Country is really world wide ***remarks: These addresses were issued by *** The IANA before the formation of *** Regional Internet Registries. *** <http://www.iana.org/assignments/ipv4-address-space> ***org: ORG-NCC1-RIPE ***admin-c: iana1-RIPE ***tech-c: iana1-RIPE ***status: ALLOCATED UNSPECIFIED ***mnt-by: RIPE-NCC-HM-MNT ***mnt-lower: RIPE-NCC-HM-MNT ***mnt-routes: RIPE-NCC-RPSL-MNT ***source: RIPE # Filtered querylist size:17 ip:129.42.58.216 inetnum:129.0.0.0 - 129.255.255.255 netname:EU-ZZ-129 descr:Various Registries country:EU # Country is really world wide remarks:These addresses were issued byThe IANA before the formation ofRegional Internet Registries. <http://www.iana.org/assignments/ipv4-address-space> org:ORG-NCC1-RIPE admin-c:iana1-RIPE tech-c:iana1-RIPE status:ALLOCATED UNSPECIFIED mnt-by:RIPE-NCC-HM-MNT mnt-lower:RIPE-NCC-HM-MNT mnt-routes:RIPE-NCC-RPSL-MNT source:RIPE # Filtered |
原创文章,转载请注明: 转载自micmiu – 软件开发+生活点滴[ http://www.micmiu.com/ ]
0 条评论。