0

I'm making a whois in java for android to train about streams and tcp connections.

But I have a problem. I have a php script, I wrote some time ago and I´m trying to make the same in java.

this is my java code :

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

The answer of the server is not correct and I think the problem is that I'm sending a bad query. The query I send is "dominio.com\r\n" and in my php whois code, it works perfectly.

Vaandu
  • 4,857
  • 12
  • 49
  • 75
user1185430
  • 35
  • 2
  • 7

2 Answers2

3

It seems that the DNS query matches multiple records. At least, that is how I interpret the response. In the returned reponse you should see the following line:

To single out one record, look it up with "xxx", where xxx is one of the of the records displayed above. If the records are the same, look them up with "=xxx" to receive a full display for each record.

So if you prepend the query with "=" it returns the data of that record only. The following worked for me.

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

One thing to consider: Use English for method names, variables, etc. instead of Spanish. It will make your code easier to read internationally. The programming language itself also uses English words. Try to avoid a strange mix of English and your native language.

Jeroen Peeters
  • 1,974
  • 1
  • 20
  • 24
  • ok, it works, know it shows me info ... but if i ask for terra.com , it shows me terra.com.br , terra.com.pe and more domain names that have terra.com in the string name... i dont know why it is working in my php but not in java. .. – user1185430 Feb 02 '12 at 16:19
0

The lookup for dominio.com results in three matches:

  • DOMINIO.COM.BR
  • DOMINIO.COM.ASCPROBIENESTARIDSS.COM
  • DOMINIO.COM

You should specify wich one you are interested in with the query.

=dominio.com<newline>

This will allways work, even cases where there are no multiple matches.

Xyz
  • 5,955
  • 5
  • 40
  • 58
  • ok, i say "dominio.com" to say an example of the query structure. i have tried with terra.com, that i know is registered, and it says there is no info about this domain ... – user1185430 Feb 02 '12 at 15:34
  • 1
    You should check out Jeroen Peeters answer, you should put "=" in front of your request. I missinterpreted the server response as "no domain found", while it in fact was "multiple domains found". – Xyz Feb 02 '12 at 15:55
  • Nitpick: per RFC3912 each line must be terminated by CR+LF – Patrick Mevzek Jan 04 '18 at 15:00