java whois_Java如何实现简单的whois查询

本文介绍了如何使用Java通过socket实现WHOIS查询功能,详细展示了WhoisBean和WhoisQuery两个核心类的代码实现,用于根据IP地址获取注册信息。
摘要由CSDN通过智能技术生成

Java如何实现简单的whois查询

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

源代码直接下载:java_whois.zip

相关的两个类WhoisBean.java 和WhoisQuery.java 代码实现如下:

Java代码

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 map = new LinkedHashMap();

try {

WhoisBean bean = new WhoisBean();

bean.setTimeout(0);

// bean.setServer("whois.apnic.net");

bean.setServer("whois.ripe.net");

bean.queryInfoByIp(ip);

List 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 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);

}

}

Java代码

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 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);

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 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 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.

***

***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.

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

【Java如何实现简单的whois查询】相关文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值