Java如何实现简单的whois查询
来源:才华咖 本文已影响2.47W人
来源:才华咖 本文已影响2.47W人
利用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
JavaScript简单实现放大镜效果代码
圣诞节英语诗歌《We Won't Have a Christmas This Year》
JavaScript中push(),join() 函数实例详解
Javascript到PHP加密通讯的简单实现
如何使用JavaScript实现倒计时
如何实现JavaScript的DIV块来回滚动效果
javascript是如何实现命名空间效果
使用JavaScript实现Java的List功能
用 Javascript 实现锚点(Anchor)间平滑跳转
深入解析hashmap,java实现原理
如何基于JavaScript实现本地图片预览
java与javascript之间json格式数据互转详解
Javascript到PHP加密通讯的简单实现方法
如何用Java创建一个简单的Swing应用程序
Java如何实现一个简单的缓存
windows下JAVA敏捷开发环境搭建步骤
如何灵活的理解JavaScript中的this指向
如何实现bootstrap jquery dataTable异步ajax刷新表格数据
JAVA中throws和throw有什么区别
javascript解析json实例
javascript学习笔记:window对象使用介绍
java如何使用poi对Execl简单写操作
JavaScript重置表单的实现
讲解Java的Spring框架中的AOP实现
JavaScript与java语言有何区别
如何解决ghost安装系统本地硬盘显示invalid switch fmount问题
如何写现实表现
大学专业如何查询
Java中hashmap和hashtable的区别
如何实现javascript去除字符串里中文与空格
咨询医生简历如何写
JavaScript与java语言有何不同
Java编程中throw和throws子句的使用方法
Java如何通过Socket实现TCP服务端
如何使用JavaScript实现香港身份格式及真伪性验证
Windows Vista如何完全安装硬盘
java与JavaScript语言有何不同
JavaScript在Android的WebView中parseInt函数转换不正确
Java如何调用WebService
Java如何实现简单的whois查询