C#查询Whois代码

using  System;
using  System.Data;
using  System.Configuration;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  System.Net;
using  System.Net.Sockets;
using  System.Text;
public  partial  class  _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{

    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
string _r = Whois(TextBox1.Text.Trim());
        
        Response.Write(_r.Replace(
"\r\n","<br>"));
    }


    
/// <summary>
    
/// 域名注册查询
    
/// </summary>
    
/// <param name="domain">输入域名,不包含www</param>
    
/// <returns></returns>

    public static string Whois(string domain)
    
{
        
if (domain == null)
            
throw new ArgumentNullException();
        
int ccStart = domain.LastIndexOf(".");
        
if (ccStart < 0 || ccStart == domain.Length)
            
throw new ArgumentException();
        
string ret = "";
        Socket s 
= null;
        
try
        
{
            
string cc = domain.Substring(ccStart + 1);
            s 
= new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            s.Connect(
new IPEndPoint(Dns.Resolve(cc + ".whois-servers.net").AddressList[0], 43));
            s.Send(Encoding.ASCII.GetBytes(domain 
+ "\r\n"));
            
byte[] buffer = new byte[1024];
            
int recv = s.Receive(buffer);
            
while (recv > 0)
            
{
                ret 
+= Encoding.ASCII.GetString(buffer, 0, recv);
                recv 
= s.Receive(buffer);
            }

            s.Shutdown(SocketShutdown.Both);
        }

        
catch
        
{
            
throw new SocketException();
        }

        
finally
        
{
            
if (s != null)
                s.Close();
        }

        
return ret;
    }

}

你可能感兴趣的:(whois)