/*
HttpContext.Current.Response.Write(Whois.GetWhoisInfo("f.com"));
*/
public class Whois
{
public static string GetWhoisInfo( string strDomain)
{
try
{
string result = "" ;
string server = " whois.crsnic.net " ;
string domain = GetDomain(strDomain);
if (domain == null )
{
return " Please enter a valid domain name. " ;
}
else
{
string end = domain.Substring(domain.LastIndexOf( " . " ));
switch (end)
{
case " .biz " :
case " .info " :
case " .mil " :
server = " whois.networksolutions.com " ;
break ;
case " .cn " :
server = " whois.cnnic.net.cn " ;
break ;
case " .edu " :
server = " whois.educause.net " ;
break ;
case " .org " :
server = " whois.publicinterestregistry.net " ;
break ;
case " .gov " :
server = " whois.nic.gov " ;
break ;
case " .hk " :
server = " whois.hkdnr.net.hk " ;
break ;
case " .name " :
server = " whois.nic.name " ;
break ;
default :
server = " whois.crsnic.net " ;
break ;
}
result = GetWhoisInfo(server, domain);
if (result.IndexOf( " no data found " ) >= 0 || result.IndexOf( " No match for \ "" + domain.ToUpper() + " \ " . " ) > 0 )
{
result = GetWhoisInfo( " Whois.internic.net " , domain);
}
Regex reg = new Regex( @" Whois\s?Server\s?\:\s?(.*?)[\n|\r] " , RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match mt = reg.Match(result);
if (String.IsNullOrEmpty(mt.Value))
{
return result;
}
else
{
string trueService = mt.Groups[ 1 ].Value.Trim();
if (trueService == server) return result;
else return GetWhoisInfo(trueService, domain);
}
}
}
catch (Exception e)
{
return e.Message;
}
}
public static string GetWhoisInfo( string server, string strDomain)
{
strDomain = GetDomain(strDomain);
if (strDomain == null ) return " Domain is null or error! " ;
string result = "" ;
UTF8Encoding utf8 = new UTF8Encoding();
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(server, 43 );
NetworkStream networkStream = tcpClient.GetStream();
strDomain = strDomain.Replace( " 0x00 " , "" );
byte [] buffer = Encoding.GetEncoding( " GB2312 " ).GetBytes(strDomain + " \r\n " );
networkStream.Write(buffer, 0 , buffer.Length);
buffer = new byte [ 10240 ];
int i = networkStream.Read(buffer, 0 , buffer.Length);
while (i > 0 )
{
i = networkStream.Read(buffer, 0 , buffer.Length);
result += utf8.GetString(buffer);
}
networkStream.Close();
tcpClient.Close();
result = result.Replace( " \u0000 " , "" );
return result;
}
public static string GetDomain( string strDomain)
{
if (String.IsNullOrEmpty(strDomain)) return null ;
string domain = strDomain.Trim().ToLower();
if (domain.StartsWith( " www. " ))
{
return domain.Substring( 4 , domain.Length - 4 );
}
if (domain.IndexOf( " . " ) < 0 ) domain += " .com " ;
if (domain == "" || domain.IndexOf( " , " ) != - 1 || domain.IndexOf( " ; " ) != - 1 || domain.IndexOf( " _ " ) != - 1 || domain.IndexOf( " . " ) == - 1 )
{
return null ;
}
return domain;
}
}
HttpContext.Current.Response.Write(Whois.GetWhoisInfo("f.com"));
*/
public class Whois
{
public static string GetWhoisInfo( string strDomain)
{
try
{
string result = "" ;
string server = " whois.crsnic.net " ;
string domain = GetDomain(strDomain);
if (domain == null )
{
return " Please enter a valid domain name. " ;
}
else
{
string end = domain.Substring(domain.LastIndexOf( " . " ));
switch (end)
{
case " .biz " :
case " .info " :
case " .mil " :
server = " whois.networksolutions.com " ;
break ;
case " .cn " :
server = " whois.cnnic.net.cn " ;
break ;
case " .edu " :
server = " whois.educause.net " ;
break ;
case " .org " :
server = " whois.publicinterestregistry.net " ;
break ;
case " .gov " :
server = " whois.nic.gov " ;
break ;
case " .hk " :
server = " whois.hkdnr.net.hk " ;
break ;
case " .name " :
server = " whois.nic.name " ;
break ;
default :
server = " whois.crsnic.net " ;
break ;
}
result = GetWhoisInfo(server, domain);
if (result.IndexOf( " no data found " ) >= 0 || result.IndexOf( " No match for \ "" + domain.ToUpper() + " \ " . " ) > 0 )
{
result = GetWhoisInfo( " Whois.internic.net " , domain);
}
Regex reg = new Regex( @" Whois\s?Server\s?\:\s?(.*?)[\n|\r] " , RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match mt = reg.Match(result);
if (String.IsNullOrEmpty(mt.Value))
{
return result;
}
else
{
string trueService = mt.Groups[ 1 ].Value.Trim();
if (trueService == server) return result;
else return GetWhoisInfo(trueService, domain);
}
}
}
catch (Exception e)
{
return e.Message;
}
}
public static string GetWhoisInfo( string server, string strDomain)
{
strDomain = GetDomain(strDomain);
if (strDomain == null ) return " Domain is null or error! " ;
string result = "" ;
UTF8Encoding utf8 = new UTF8Encoding();
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(server, 43 );
NetworkStream networkStream = tcpClient.GetStream();
strDomain = strDomain.Replace( " 0x00 " , "" );
byte [] buffer = Encoding.GetEncoding( " GB2312 " ).GetBytes(strDomain + " \r\n " );
networkStream.Write(buffer, 0 , buffer.Length);
buffer = new byte [ 10240 ];
int i = networkStream.Read(buffer, 0 , buffer.Length);
while (i > 0 )
{
i = networkStream.Read(buffer, 0 , buffer.Length);
result += utf8.GetString(buffer);
}
networkStream.Close();
tcpClient.Close();
result = result.Replace( " \u0000 " , "" );
return result;
}
public static string GetDomain( string strDomain)
{
if (String.IsNullOrEmpty(strDomain)) return null ;
string domain = strDomain.Trim().ToLower();
if (domain.StartsWith( " www. " ))
{
return domain.Substring( 4 , domain.Length - 4 );
}
if (domain.IndexOf( " . " ) < 0 ) domain += " .com " ;
if (domain == "" || domain.IndexOf( " , " ) != - 1 || domain.IndexOf( " ; " ) != - 1 || domain.IndexOf( " _ " ) != - 1 || domain.IndexOf( " . " ) == - 1 )
{
return null ;
}
return domain;
}
}