java whois_如何用Java实现简单whois查询

展开全部

package michael.socket;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.io.PrintStream;

import java.net.Socket;

import java.util.ArrayList;

import java.util.LinkedHashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

/**

* whois查询

* @author michael

* @see http://sjsky.iteye.com

*/

public class WhoisQueryHandler {

/**

* whois.apnic.net

* whois.ripe.net

*/

private String server ="whois.ripe.net";

/**

* port

*/

private int port = 43;

/**

* timeout/minute

*/

private int timeout = 0;

/**

* @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;

}

/**

* @param ipOrDomain

* @return List

*/

public List queryInfoListByWhois(String ipOrDomain) {

Socket qrySocket = null;

BufferedReader br = null;

PrintStream ps = null;

int qryCount = 0;

List infoList = new ArrayList();

infoList.add("查询的32313133353236313431303231363533e4b893e5b19e31333337623537对象:"+ ipOrDomain);

while (qryCount 

qryCount++;

try {

qrySocket = new Socket(server, port);

qrySocket.setSoTimeout(timeout * 1000);

ps = new PrintStream(qrySocket.getOutputStream());

ps.println(ipOrDomain);

br = new BufferedReader(new InputStreamReader(qrySocket

.getInputStream()));

String readLine = null;

int lineCount = 0;

System.out.println("Whois query start....");

while ((readLine = br.readLine()) != null && lineCount 

System.out.println("readLine ["+ readLine +"]");

if ("".equals(readLine) || readLine.charAt(0) == '%') {

continue;

}

lineCount++;

infoList.add(readLine);

}

System.out.println("whois query info finish");

break;

} catch (Exception e) {

e.printStackTrace();

} finally {

if (null != br) {

try {

br.close();

} catch (Exception e1) {

}

}

if (null != ps) {

try {

ps.close();

} catch (Exception e1) {

}

}

if (null != qrySocket) {

try {

qrySocket.close();

} catch (Exception e1) {

}

}

}

}

return infoList;

}

/**

* @param ipOrDomain

* @return List

*/

public Map queryInfoMapByWhois(String ipOrDomain) {

List infoList = this.queryInfoListByWhois(ipOrDomain);

Map infoMap = new LinkedHashMap();

String key ="";

String value ="";

for (String info : infoList) {

if (info.startsWith("") || info.indexOf(':') == -1) {

value = info;

} else {

key = info.substring(0, info.indexOf(':'));

value = info.substring(info.indexOf(':') + 1);

}

if (null == infoMap.get(key)) {

infoMap.put(key, value);

} else {

infoMap.put(key, infoMap.get(key) + value);

}

}

return infoMap;

}

/**

* @param args

*/

public static void main(String[] args) {

WhoisQueryHandler handler = new WhoisQueryHandler();

/**

*"218.202.224.2"

*"180.168.130.146

* iteye.com

* AS9808

*/

String ipOrDomain ="129.42.58.216";

Map map = handler.queryInfoMapByWhois(ipOrDomain);

for (Entry entry : map.entrySet()) {

System.out.println(entry.getKey() +":"+ entry.getValue());

}

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值