关闭

Java实现简单whois查询

发表于:2010-6-17 10:40

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

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

32/3<123>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号