1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
101 102 103 104 105 106 107 108 109 110
111 112 113 114 115 116 117 118 119 120
121 122 123 124 125 126 127 128 129 130
131 132 133 134 135 136 137 138 139
|
import java.io.*;
import java.net.*;
/**
* @author Neeraj Vora
*
* WHOIS gets domain and server information.
* Talks to Internic sever to get domain registration information.
* Talks to server to get daytime and finger-query information.
*/
public class WHOIS
{
/**
* Private method to talk to a specific host-port combination.<br>
* Uses lineSepartor to delimit the response lines in the return
* string.
*/
private String talkToHostSocket (String hostName, int portNumber,
String sendString, String lineSeparater,
String exceptionMessagePrefix)
{
StringBuffer returnStringBuffer = new StringBuffer();
String line;
try
{
Socket s = new Socket (hostName, portNumber);
PrintWriter pw = new PrintWriter (s.getOutputStream(), true);
pw.println (sendString);
BufferedReader br = new BufferedReader
(new InputStreamReader(s.getInputStream()));
while ((line=br.readLine())!=null)
{
returnStringBuffer.append (line);
returnStringBuffer.append (lineSeparater);
}
}
catch (Exception e)
{
returnStringBuffer.append (exceptionMessagePrefix);
returnStringBuffer.append (e.toString());
}
return returnStringBuffer.toString();
} // end talkToHostSocket
/**
* Talks to Internic server at port 43 to inquire about domain
* registration.
*/
public String getDomainInfo (String domainName)
{
return talkToHostSocket ("whois.internic.net", 43, domainName, "\n",
"Error Getting Domain Information:");
} // end getDomainInfo
/**
* Talks to Internic server at port 43 to inquire about domain
* registration.<br>
* Uses lineSepartor to delimit the response lines in the return
* string.<br>
* Towards the end of 1999, the Internic server only points to one
* of the several possible competing registrars, and the WHOIS server
* of the registrar actually provides the registration information.
* <br>
* The method now automatically goes to the appropriate registrar.
* It also looks up the first record in case of multi-record result.
*/
public String getDomainInfo (String domainName, String lineSeparater)
{
String str = talkToHostSocket ("whois.internic.net", 43, domainName, lineSeparater,
"Error Getting Domain Information:");
int i = str.indexOf ("Whois Server:");
if (i<0)
return str;
i = str.indexOf (":",i);
if (i<0)
return str;
int j = str.indexOf (lineSeparater, i);
if (j<=i)
return str;
String str2 = str.substring(i+1,j).trim();
StringBuffer sb = new StringBuffer(str);
sb.append (lineSeparater);
sb.append (lineSeparater);
sb.append (lineSeparater);
String str3 = talkToHostSocket (str2, 43, domainName, lineSeparater,
"Error Getting Domain Information:");
sb.append (str3);
i = str3.indexOf ("!xxx");
if (i<0)
return sb.toString();
sb.append (lineSeparater);
sb.append (lineSeparater);
sb.append (lineSeparater);
i = str3.indexOf ("-DOM");
if (i<0)
return sb.toString();
String str4 = str3.substring (0, i);
i = str4.lastIndexOf ("(");
if (i<0)
return sb.toString();
str4 = "!" + str4.substring (i+1) + "-DOM";
sb.append (talkToHostSocket (str2, 43, str4, lineSeparater,
"Error Getting Domain Information:"));
return sb.toString();
} // end getDomainInfo
/**
* Talks to the server at port 13 to get the daytime information.
*/
public String getDaytimeInfo (String hostName)
{
return talkToHostSocket (hostName, 13, "daytime", "",
"Error Getting Server Time:");
} // end getDaytimeInfo
/**
* Talks to the server at port 79 to get the finger query
* information.
*/
public String getFingerQueryInfo (String hostName, String userName)
{
return talkToHostSocket (hostName, 79, userName, "\n",
"Error Getting User Information:");
} // end getFingerQueryInfo
/**
* Talks to the server at port 79 to get the finger query
* information.<br>
* Uses lineSepartor to delimit the response lines in the return
* string.
*/
public String getFingerQueryInfo (String hostName, String userName, String lineSeparater)
{
return talkToHostSocket (hostName, 79, userName, lineSeparater,
"Error Getting User Information:");
} // end getFingerQueryInfo
} // end class WHOIS
|