域名查询功能

 关键代码:

public string Lookup(string strDomain)
    {
        try
        {
            string result = "";
            string server = "whois.internic.net";
            string ext = ".com";
            int port = 43;
            int address = -1;
            string domain = strDomain.Trim().ToLower();

            if (domain.StartsWith("www."))
            {
                domain = domain.Substring(4, domain.Length - 4);
            }

            if (domain == "" || domain.IndexOf(",") != -1 || domain.IndexOf(";") != -1 || domain.IndexOf("_") != -1 || domain.IndexOf(".") == -1)
            {
                result = "Please enter a valid domain name.";
            }
            else
            {
                address = domain.IndexOf(".");
                ext = domain.Substring(address);

                switch (ext)
                {
                    case ".com":
                    case ".net":
                    case ".org":
                    default:
                        server = "whois.crsnic.net";
                        //server="whois.internic.net";
                        break;
                    case ".biz":
                    case ".info":
                    case ".mil":
                        server = "whois.networksolutions.com";
                        break;
                    case ".edu":
                        server = "whois.educause.net";
                        break;
                    case ".cn":
                    case ".com.cn":
                    case ".net.cn":
                    case ".org.cn":
                    //case ".gov.cn":

                    case ".bj.cn":
                    case ".sh.cn":
                    case ".tj.cn":
                    case ".cq.cn":
                    case ".he.cn":
                    case ".nm.cn":
                    case ".ln.cn":
                    case ".jl.cn":
                    case ".hl.cn":
                    case ".js.cn":
                    case ".zj.cn":
                    case ".ah.cn":
                    case ".hb.cn":
                    case ".hn.cn":
                    case ".gd.cn":
                    case ".gx.cn":
                    case ".hi.cn":
                    case ".sc.cn":
                    case ".gz.cn":
                    case ".yn.cn":
                    case ".xz.cn":
                    case ".sn.cn":
                    case ".gs.cn":
                    case ".qh.cn":
                    case ".nx.cn":
                    case ".xj.cn":
                    case ".tw.cn":
                    case ".hk.cn":
                    case ".mo.cn":
                        server = "whois.cnnic.net.cn";
                        break;
                }

                //starting lookup
                UTF8Encoding utf8 = new UTF8Encoding();
                // Connect to the whois server
                TcpClient tcpClient = new TcpClient();
                tcpClient.Connect(server, port);
                NetworkStream networkStream = tcpClient.GetStream();
                // Send the domain name to the whois server

                domain = domain.Replace("0x00", "");
                byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(domain + "\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;
        }

        catch (Exception e)
        {
            return e.Message;
            //return "Sorry.Lookup is false.Please check the format of domain or internet connection.";
        }

    }

查询按钮事件:

protected void btnImgCX_Click(object sender, ImageClickEventArgs e)
    {
        string domain = tbxDomain.Value.ToString().Trim();
        StringBuilder sbext = new StringBuilder();

        string result = null, AllDomain = null;
        sbext.Append("<table class='tr' border='0' width='80%' style='border:1px solid red'><tr><td>域名</td><td>查询结果</td></tr>");
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                AllDomain = domain + item.Value;
                result = Lookup(AllDomain).Replace('\n', ' ').Trim().Substring(0, 6);
                sbext.Append("<tr><td>"+AllDomain+"</td>");
                if (result.CompareTo("Domain") == 0)
                {
                    //相等-已注册
                    sbext.Append("<td>已注册</td></tr>");
                }
                else
                {
                    sbext.Append("<td>可以注册</td></tr>");
                }
               // sbext.Append("<br/>");
            }
        }
              //string domainExt = cblDomainExt.SelectedItem.Text.ToString();
        sbext.Append("</table>");
        Session["rt"] = sbext.ToString();
        Response.Redirect("YuMingResult.aspx");
    }

页面部分代码:

 <div style="float:left">
      <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal">
      <asp:ListItem Value=".com">.com</asp:ListItem>
      <asp:ListItem Value=".cn">.cn</asp:ListItem>
      <asp:ListItem Value=".net">.net</asp:ListItem>
      </asp:CheckBoxList>
 </div>

 

posted @ 2011-03-07 16:55  双魂人生  阅读(465)  评论(0编辑  收藏  举报