C#域名查询代码

代码
 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebStat.WebForm2" %>
 2 
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml" >
 6 <head runat="server">
 7     <title>分享C#域名查询代码</title>
 8 </head>
 9 <body>
10     <form id="form1" runat="server">
11     <div>
12         www.<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1"
13             runat="server" Text="Button" OnClick="Button1_Click" />
14         <br />
15         <div runat="server" id="info"></div>
16         <br />
17         <div runat="server" id="xml"></div>
18     </div>
19     </form>
20 </body>
21 </html>

 

 

代码
  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Collections;
  5 using System.Web;
  6 using System.Web.Security;
  7 using System.Web.UI;
  8 using System.Web.UI.WebControls;
  9 using System.Web.UI.WebControls.WebParts;
 10 using System.Web.UI.HtmlControls;
 11 using System.Net.Sockets;
 12 using System.Text;
 13 using System.Net;
 14 using System.IO;
 15 using System.Xml;
 16 
 17 namespace WebStat
 18 {
 19     public partial class WebForm2 : System.Web.UI.Page
 20     {
 21         /// <summary>
 22         /// 
 23         /// </summary>
 24         /// <param name="sender"></param>
 25         /// <param name="e"></param>
 26         protected void Page_Load(object sender, EventArgs e)
 27         {
 28             //查获询域名IP
 29             string strDomain = "www.dupcit.com";
 30             IPHostEntry hostEntry = Dns.Resolve(strDomain);
 31             IPEndPoint IPEndPoint = new IPEndPoint(hostEntry.AddressList[0], 0);          
 32             string ipAddress = IPEndPoint.Address.ToString();
 33             Response.Write("IP:"+ipAddress);
 34 
 35             Response.Write(IsReg(strDomain).ToString());
 36             //这里注意判断是有有来源页面
 37             string refHost = Request.ServerVariables["http_referer"] ;//== null ? "" : HttpContext.Current.Request.UrlReferrer.Host.ToLower(); // HttpContext.Current.Request.UrlReferrer == null ? "" : HttpContext.Current.Request.UrlReferrer.Host.ToLower();
 38             xml.InnerHtml = refHost;
 39 
 40             //if (refHost != "" && refHost.IndexOf("dupcit.com") == -1)//如果是你自己的域名不用统计
 41         
 42             //if (refHost.StartsWith("www.")) refHost = refHost.Substring(4);//去掉www.
 43         }
 44         /// <summary>
 45         /// 
 46         /// </summary>
 47         /// <param name="sender"></param>
 48         /// <param name="e"></param>
 49         protected void Button1_Click(object sender, EventArgs e)
 50         {
 51             string _r = whois(this.TextBox1.Text.Trim());            
 52             //Response.Write(_r);//.Replace("\r\n", "<br>")
 53             info.InnerHtml = _r.ToString();
 54 
 55             //string name1 = "www.dupcit.com";// HttpContext.Current.Server.UrlEncode(TextBox1.Text.Trim());
 56             //WebClient wc = new WebClient();
 57             //string xmlstr = wc.DownloadString("http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + name1);
 58             //xml.InnerHtml = xmlstr;
 59             //if (xmlstr.IndexOf("200") > -1)
 60             //{
 61             //    if (xmlstr.IndexOf("210") > -1)
 62             //    {
 63             //        xml.InnerHtml += "可以注册";
 64             //    }
 65             //    else if (xmlstr.IndexOf("211") > -1)
 66             //    {
 67             //        xml.InnerHtml += "已经被注册";
 68             //    }
 69             //    else if (xmlstr.IndexOf("212") > -1)
 70             //    {
 71             //        xml.InnerHtml += "参数错误";
 72             //    }
 73             //    else
 74             //    {
 75             //        xml.InnerHtml += "其他错误";
 76             //    }
 77             //}
 78             //else
 79             //{
 80             //    xml.InnerHtml += "返回出错!";
 81             //}
 82 
 83         }
 84         /**/
 85         /// <summary>
 86         /// 域名注册查询
 87         /// </summary>
 88         /// <param name="domain">输入域名,不包含www</param>
 89         /// <returns></returns>
 90         public static string whois(string domain)
 91         {
 92             if (domain == null)
 93                 throw new ArgumentNullException();
 94             int ccstart = domain.LastIndexOf(".");
 95             if (ccstart < 0 || ccstart == domain.Length)
 96                 throw new ArgumentException();
 97             string ret = "";
 98             Socket s = null;
 99             try
100             {
101                 string cc = domain.Substring(ccstart + 1);
102                 s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
103                 s.Connect(new IPEndPoint(Dns.Resolve(cc + ".whois-servers.net").AddressList[0], 43));
104                 s.Send(Encoding.ASCII.GetBytes(domain + "\r\n"));
105                 byte[] buffer = new byte[1024];
106                 int recv = s.Receive(buffer);
107                 while (recv > 0)
108                 {
109                     ret += Encoding.ASCII.GetString(buffer, 0, recv);
110                     recv = s.Receive(buffer);
111                 }
112                 s.Shutdown(SocketShutdown.Both);
113             }
114             catch
115             {
116                 throw new SocketException();
117             }
118             finally
119             {
120                 if (s != null)
121                     s.Close();
122             }
123             return ret;
124         }
125         /// <summary>
126         /// 
127         /// </summary>
128         /// <param name="domainName"></param>
129         /// <returns></returns>
130       public static bool IsReg(string domainName)
131      {
132         bool flag = false;
133         string dm = HttpContext.Current.Server.UrlEncode(domainName);
134         try
135         {
136         //判断方法非常多,如打开远程文件再处理字符串等等,这里用的方法效率不是很高
137             WebClient wc = new WebClient();
138             string xmlstr = wc.DownloadString("http://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + dm);
139             StringReader sr = new StringReader(xmlstr);
140             XmlTextReader xr = new XmlTextReader(sr);            
141             while (xr.Read())
142             {
143                 if (xr.IsStartElement("original"))
144                 {
145                     xr.Read();
146                     if (xr.Value.Substring(03== "210")
147                     {
148                         flag = true;
149                         break;
150                     }
151                     else
152                     {
153                         flag = false;
154                         break;
155                     }
156                 }
157             }
158             return flag;
159         }
160         catch
161        {
162             return false;
163         }
164     }
165     }
166 }
167 

 

 

 

posted @ 2010-12-15 17:08  ®Geovin Du Dream Park™  阅读(1109)  评论(0编辑  收藏  举报