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
-
SUN认证考试知识点:Java反射机制的应用
目标:用一个代理类实现两个相似类的调用1.两个相似类Man类Women类:代理类:packageReflect;importod;publicclassPerson{//代理类privateObjecttarget;//目标对象privateStringmethodName;//目标方法privateObject[]params;//参数数组privateMethodmethod;publicOb...
-
关于J2ME数组的复制及连接操作方法
publicclassArrays{/***构造函数私有,这样可以保证只能通过:类名.静态方法或类名.静态方法来访问内部数据,*而不可以通过创建本类的对象来进行访问*/privateArrays(){}/***复制一个跟源byte数组一样的byte数组*@paramrSource源byte数组*@return跟源byte[]数组一...
-
Sun目前的软件策略
作为Sun一直执行的开源其整个中间件软件产品包战略的组成部分,Sun将发布JavaEnterpriseSystem的下一代组件。Sun将通过互联网上的OpenJava门户项目把SunJavaSystemPortalServer7(系统门户服务器第7版)中的所有组件和技术发布出来。一开始,Sun将创建一个开源Portl...
-
sun认证考试经验:多线程的几种实现方法详解
随着CPU主频的不断攀升,X86架构的硬件已经成为瓶,在这种架构的CPU主频最高为4G。事实上目前3.6G主频的CPU已经接近了顶峰。如果不能从根本上更新当前CPU的架构(在很长一段时间内还不太可能),那么继续提高CPU性能的方法就是超线程CPU模式。那么,作业系统、应用程序...