| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 623 人关注过本帖
标题:用java编写的whois查询程序
只看楼主 加入收藏
isolated
Rank: 1
来 自:北京
等 级:新手上路
帖 子:23
专家分:0
注 册:2009-3-23
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
用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 Isolated

  */

  public class WhoisQuery {

  /**

  *

  * @param ip

  */

  private void queryIpInfo(String ip) {

  Map<String, String> map = new LinkedHashMap<String, String>();

  try {

  WhoisBean bean = new WhoisBean();

  bean.setTimeout(0);

  // bean.setServer("whois.);

  bean.setServer("whois.);

  bean.queryInfoByIp(ip);

  List<String> 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<String, String> 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

  import

  import

  import

  import java.util.ArrayList;

  import java.util.List;

  /**

  * WhoisBean

  * @author website:http://www.

  */

  public class WhoisBean {

  /**

  * server address

  */

  private String server = "";

  /**

  * port

  */

  private int port = 43;

  /**

  * timeout/minute

  */

  private int timeout = 0;

  /**

  * infoList

  */

  private List<String> infoList = new ArrayList<String>();

  /**

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

  }

  }

  }

搜索更多相关主题的帖子: 源代码 IP地址查询 java Java 
2011-01-26 10:06
isolated
Rank: 1
来 自:北京
等 级:新手上路
帖 子:23
专家分:0
注 册:2009-3-23
收藏
得分:0 
 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<String> getInfoList() {

  return infoList;

  }

  /**

  * @param pInfoList the infoList to set

  */

  public void setInfoList(List<String> 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.

  ***

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

  ***              <http://www.

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

  <http://www.

  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



http://www.
2011-01-26 10:06
虾B写
Rank: 8Rank: 8
来 自:湖北
等 级:蝙蝠侠
威 望:3
帖 子:395
专家分:922
注 册:2009-10-1
收藏
得分:20 
人漂亮,代码也漂亮

白娘故意下雨骗许仙的伞。祝英台十八里相送时装疯卖傻调戏梁山伯。七仙女挡住了董永的去路。牛郎趁织女洗澡时拿走了她的衣服。。。这些故事告诉我们;伟大爱情的开始,总归的有一个要先耍流氓!
2011-01-26 10:33
快速回复:用java编写的whois查询程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016612 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved