0

我正在为 android 制作一个 java 中的 whois 来训练有关流和 tcp 连接的信息。

但我有一个问题。我有一个 php 脚本,我前段时间写过,我正在尝试在 java 中做同样的事情。

这是我的java代码:

 public String consultawhois(String domain,String tld)
    {
        String domquest = domain + "." + tld;
        String resultado = "";
        Socket theSocket;
        String hostname = "whois.internic.net";
        int port = 43;
        try {
          theSocket = new Socket(hostname, port, true);
          Writer out = new OutputStreamWriter(theSocket.getOutputStream());
          out.write(domquest + "\r\n");
          out.flush();
          DataInputStream theWhoisStream;
          theWhoisStream = new DataInputStream(theSocket.getInputStream());
          String s;
          while ((s = theWhoisStream.readLine()) != null) {
            resultado = resultado + s + "\n";
          }
        }
        catch (IOException e) {
        }

        return resultado;
    }

服务器的答案不正确,我认为问题在于我发送了错误的查询。我发送的查询是“dominio.com\r\n”,在我的 php whois 代码中,它运行良好。

4

2 回答 2

3

似乎 DNS 查询匹配多条记录。至少,这就是我解释响应的方式。在返回的响应中,您应该看到以下行:

要挑出一条记录,请使用“xxx”进行查找,其中 xxx 是上面显示的记录之一。如果记录相同,请使用“=xxx”查找它们以接收每条记录的完整显示。

因此,如果您在查询前加上“=”,它只会返回该记录的数据。以下对我有用。

public String consultawhois(String domain,String tld)
{
    String domquest = domain + "." + tld;
    String resultado = "";
    Socket theSocket;
    String hostname = "whois.internic.net";
    int port = 43;
    try {
      theSocket = new Socket(hostname, port, true);
      Writer out = new OutputStreamWriter(theSocket.getOutputStream());
      out.write("="+domquest + "\r\n");
      out.flush();
      DataInputStream theWhoisStream;
      theWhoisStream = new DataInputStream(theSocket.getInputStream());
      String s;
      while ((s = theWhoisStream.readLine()) != null) {
        resultado = resultado + s + "\n";
      }
    }
    catch (IOException e) {
    }

    return resultado;
}

需要考虑的一件事:使用英语作为方法名称、变量等,而不是西班牙语。这将使您的代码更容易在国际上阅读。编程语言本身也使用英语单词。尽量避免英语和您的母语的奇怪混合。

于 2012-02-02T15:39:47.787 回答
0

对 dominio.com 的查找会产生三个匹配项:

  • 多米尼奥.COM.BR
  • DOMINIO.COM.ASCPROBIENESTARIDSS.COM
  • 多米尼奥网

您应该指定您对查询感兴趣的那个。

=dominio.com<newline>

即使没有多个匹配项,这也将始终有效。

于 2012-02-02T15:25:18.237 回答