Java如何实现简单的whois查询
利用Java socket 实现根据IP地址查询该IP相关的注册信息。
源代码直接下载:java_
相关的两个类 和 代码实现如下:
Java代码
import edHashMap;
import ;
import ;
import y;
/**
*
* @author Michael sun
*/
public class WhoisQuery {
/**
*
* @param ip
*/
private void queryIpInfo(String ip) {
Map map = new LinkedHashMap();
try {
WhoisBean bean = new WhoisBean();
imeout(0);
// erver("");
erver("");
yInfoByIp(ip);
List infoList = nfoList();
String value = "";
String key = "";
for (String infoTmp : infoList) {
String[] arr = t(":[ ]*");
if (th > 1) {
key = arr[0];
value = arr[1];
} else {
value = arr[0]();
}
if (null == (key)) {
(key, "");
}
value = (key) + value;
(key, value);
}
} catch (Exception e) {
tStackTrace();
}
for (Entry entry : ySet()) {
tln(ey() + ":" + alue());
}
}
/**
* @param args
*/
public static void main(String[] args) {
String ip = "";// "";
WhoisQuery query = new WhoisQuery();
yIpInfo(ip);
}
}
Java代码
import eredReader;
import tStreamReader;
import tStream;
import et;
import yList;
import ;
/**
* 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 < 5) {
qryCount++;
try {
theSocket = new Socket(server, port);
oTimeout(timeout * 1000);
ps = new PrintStream(utputStream());
tln(ip);
br = new BufferedReader(new InputStreamReader(theSocket
nputStream()));
("ip:" + ip);
String readLine = "";
int i = 0;
tln("Whois query read start.... ");
while ((readLine = Line()) != null) {
tln("***" + readLine);
if (th() > 0 && At(0) != '%') {
(readLine);
i++;
// 默认读取100行数据
if (i > 100 || tsWith("source")) {
break;
}
}
}
tln("querylist size:" + ());
break;
} catch (Exception e) {
tln("EXCEPTION : " + e);
} finally {
if (null != br) {
e();
}
if (null != ps) {
e();
}
if (null != theSocket) {
e();
}
}
}
}
/**
* @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 getInfoList() {
return infoList;
}
/**
* @param pInfoList the infoList to set
*/
public void setInfoList(List pInfoList) {
infoList = pInfoList;
}
}
运行WhoisQuery这个类就可以看到如下信息:
Java代码
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
***
***% Note: This output has been filtered.
***% To receive output for a database update, use the "-B" flag.
***
***% Information related to ' - '
***
***inetnum: -
***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.
***
***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:
inetnum: -
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.
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
-
H3C中小企业无线解决方案
根据中国互联网络信息中心(CNNIC)公布的数据,截至2008年6月底,中国网民数量达到了2.53亿,首次大幅度超过美国,跃居世界第一位。中国也真正地步入网络时代。其中WLAN网络在提高企业效率、降低企业成本、提高用户满意度等方面有着突出的作用。那么随着网络使用范围的...
-
2017华为认证资深网络工程师联络中心模拟试题及答案
1、(题型:单选)eSpaceU2990是华为语音通信领域的旗舰级平台,支持()。A.宽带接入,但不支持窄带接入B.窄带接入,但不支持宽带接入C.宽窄带一体化接入,但不面向NGN网络D.宽窄带一体化接入,面向NGN网络2、(题型:单选)eSpaceU2990比通用服务器更好的保证系统的大容量、安全...
-
2016年计算机等级考试二级VFP笔试试题及答案
2016年计算机二级VF考试就要到了,为帮助大家巩固计算机VF知识点,本站小编为大家分享最新的VF考试题库及答案,仅供参考!一、选择题(每小题2分,共70分)下列各题A)、B)、C)、D)四个选项中,只有一个选项是正确的,请将正确选项涂写在答题卡相应位置上,答在试卷上不得分。(1...
-
我的JavaEE学习路线图
从学习Java开发到现在虽然也已经快三年了,但是要说到分享一下经验实在是不敢当。权当是对自己的一个总结吧,希望大家不吝指教,互相交流。照旧,还是现来整理一下我学习Java的一个路线图吧,然后按照这个路线图来谈谈我的一些感受。一、Java核心这是学习Java的基础,掌握...