首页 > 互联网 > IT认证 > SUN认证 > Java如何实现简单的whois查询

Java如何实现简单的whois查询

   来源:学问社    阅读: 2.44W 次
字号:

用手机扫描二维码 在手机上继续观看

手机查看

利用Java socket 实现根据IP地址查询该IP相关的'注册信息。

Java如何实现简单的whois查询

代码直接下载: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

EDA技术
嵌入式
J2EE
ACCP
JAVA认证
SUN认证
Linux认证
Adobe认证