[C#]域名的Whois信息查询

研究了一天,终于可以拿出来晒晒了。

刚开始,我一直用:whois.crsnic.net 查询 .com的域名,可查询几次后发现,很多查不到,于是仔细读了一下返回的信息,

 


   Domain Name: FUTE53.COM
   Registrar: XIN NET TECHNOLOGY CORPORATION
   Whois Server: whois.paycenter.com.cn
   Referral URL: http://www.xinnet.com
   Name Server: NS1.MYHOSTADMIN.NET
   Name Server: NS2.MYHOSTADMIN.NET
   Status: ok
   Updated Date: 22-mar-2009
   Creation Date: 22-mar-2009
   Expiration Date: 22-mar-2010

>>> Last update of whois database: Wed, 20 Jan 2010 07:10:47 UTC 
<<<

NOTICE: 
The expiration date displayed in this record is the date the 
registrar's sponsorship of the domain name registration in the registry is 
currently set to expire. This date does not necessarily reflect the expiration 
date of the domain name registrant's agreement with the sponsoring 
registrar.  Users may consult the sponsoring registrar's Whois database to 
view the registrar's reported date of expiration for this registration.

TERMS OF USE: You are not authorized to access or query our Whois 
database through the use of electronic processes that are high-volume and 
automated except as reasonably necessary to register domain names or 
modify existing registrations; the Data in VeriSign Global Registry 
Services' ("VeriSign") Whois database is provided by VeriSign for 
information purposes only, and to assist persons in obtaining information 
about or related to a domain name registration record. VeriSign does not 
guarantee its accuracy. By submitting a Whois query, you agree to abide 
by the following terms of use: You agree that you may use this Data only 
for lawful purposes and that under no circumstances will you use this Data 
to: (1) allow, enable, or otherwise support the transmission of mass 
unsolicited, commercial advertising or solicitations via e-mail, telephone, 
or facsimile; or (2) enable high volume, automated, electronic processes 
that apply to VeriSign (or its computer systems). The compilation, 
repackaging, dissemination or other use of this Data is expressly 
prohibited without the prior written consent of VeriSign. You agree not to 
use electronic processes that are automated and high-volume to access or 
query the Whois database except as reasonably necessary to register 
domain names or modify existing registrations. VeriSign reserves the right 
to restrict your access to the Whois database in its sole discretion to ensure 
operational stability.  VeriSign may restrict or terminate your access to the 
Whois database for failure to abide by these terms of use. VeriSign 
reserves the right to modify these terms at any time. 

The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.
   Domain Name: FUTE53.COM
   Registrar: XIN NET TECHNOLOGY CORPORATION
   Whois Server: whois.paycenter.com.cn
   Referral URL: http://www.xinnet.com
   Name Server: NS1.MYHOSTADMIN.NET
   Name Server: NS2.MYHOSTADMIN.NET
   Status: ok
   Updated Date: 22-mar-2009
   Creation Date: 22-mar-2009
   Expiration Date: 22-mar-2010

>>> Last update of whois database: Wed, 20 Jan 2010 07:10:47 UTC <<<

NOTICE: 
The expiration date displayed in this record is the date the 
registrar's sponsorship of the domain name registration in the registry is 
currently set to expire. This date does not necessarily reflect the expiration 
date of the domain name registrant's agreement with the sponsoring 
registrar.  Users may consult the sponsoring registrar's Whois database to 
view the registrar's reported date of expiration for this registration.

TERMS OF USE: You are not authorized to access or query our Whois 
database through the use of electronic processes that are high-volume and 
automated except as reasonably necessary to register domain names or 
modify existing registrations; the Data in VeriSign Global Registry 
Services' ("VeriSign") Whois database is provided by VeriSign for 
information purposes only, and to assist persons in obtaining information 
about or related to a domain name registration record. VeriSign does not 
guarantee its accuracy. By submitting a Whois query, you agree to abide 
by the following terms of use: You agree that you may use this Data only 
for lawful purposes and that under no circumstances will you use this Data 
to: (1) allow, enable, or otherwise support the transmission of mass 
unsolicited, commercial advertising or solicitations via e-mail, telephone, 
or facsimile; or (2) enable high volume, automated, electronic processes 
that apply to VeriSign (or its computer systems). The compilation, 
repackaging, dissemination or other use of this Data is expressly 
prohibited without the prior written consent of VeriSign. You agree not to 
use electronic processes that are automated and high-volume to access or 
query the Whois database except as reasonably necessary to register 
domain names or modify existing registrations. VeriSign reserves the right 
to restrict your access to the Whois database in its sole discretion to ensure 
operational stability.  VeriSign may restrict or terminate your access to the 
Whois database for failure to abide by these terms of use. VeriSign 
reserves the right to modify these terms at any time. 

The Registry database contains ONLY .COM, .NET, .EDU domains and
Registrars.

 

 

原来返回的信息里面给出了whois server ,连接试试,发现可用,于是就想到了利用正则的方式求正确whois所在server,然后再求whois info.

/* 
HttpContext.Current.Response.Write(Whois.GetWhoisInfo("fute53.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 == nullreturn "Domain is null or error!";
            
string result = "";
            UTF8Encoding utf8 
= new UTF8Encoding();
            
//   Connect   to   the   whois   server   
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect(server, 
43);
            NetworkStream networkStream 
= tcpClient.GetStream();
            
//   Send   the   domain   name   to   the   whois   server   
            strDomain = strDomain.Replace("0x00""");
            
byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(strDomain + "\r\n");
            
//byte[]   buffer   =   utf8.GetBytes(domain   +   "\r\n");   
            networkStream.Write(buffer, 0, buffer.Length);
            
//   Read   back   the   results   
            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);
                
//result   +=Encoding.GetEncoding("GB2312").GetString(buffer);   
            }
            networkStream.Close();
            tcpClient.Close();
            
//return   the   lookup   resutlt   
            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;
        }

    }

 

时间有限,不多写了,希望大家多给给意见

posted @ 2010-01-20 16:38  竹子小毅  阅读(1079)  评论(0编辑  收藏  举报