欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 域名注册查询接口 支持批量后缀查询

程序员文章站 2022-12-01 18:04:24
 需要用到域名查询的功能,网上查了一些资料,几乎都是ASP版的,而且功能有限,不能满足我的要求。 百度后,结合网上的例子,整理出了该功能! asp.net 域名注册查...

 需要用到域名查询的功能,网上查了一些资料,几乎都是ASP版的,而且功能有限,不能满足我的要求。

百度后,结合网上的例子,整理出了该功能!

asp.net 域名注册查询功能, 支持批量后缀查询!

要实现查询,首先要找到域名查询接口,这里我们使用的是万网的域名查询接口

 

WHOIS信息接口:

 


例如查询域名:tstmm.com

 


该网址返回的数据为XML格式:


[html]
<?xml version="1.0" encoding="gb2312"?> 
  <property> 
  <returncode>200</returncode> 
  <key>tstmm.com</key> 
  <original>210 : Domain name is available</original> 
  </property> 

<?xml version="1.0" encoding="gb2312"?>
  <property>
  <returncode>200</returncode>
  <key>tstmm.com</key>
  <original>210 : Domain name is available</original>
  </property>
返回 XML 结果说明:
returncode=200 表示接口返回成功
key=tstmm.com表示当前check的域名
original=210 : Domain name is available     表示域名可以注册
original=211 : Domain name is not available 表示域名已经注册
original=212 : Domain name is invalid   表示域名参数传输错误

 

所以我们通过分析original返的结果就可以知道该域名是否可以注册!

下面开始贴代码:

前台代码:


[csharp]
<p style="width:330px;margin-top:50px;margin-left:100px;"> 
       <p style="width:280px;margin-top:0px;">www.<input type="text" id="txtyuming" runat="server" style="width:120px;height:18px;border:1px solid #DEDEDE;"  />  <asp:Button ID="btnselect" Text="查询" runat="server" onclick="btnselect_Click" /></p> 
       <p style="line-height:25px;"> 
           <table border="0" cellpadding="0" cellspacing="0"> 
            <tr> 
               <td><input id="chkcom" name="chk" type="checkbox" checked="checked" value='.com' />.com</td> 
               <td><input id="chknet" name="chk" type="checkbox" value='.net'/>.net</td> 
               <td><input id="chkcn" name="chk" type="checkbox" value='.cn'/>.cn</td> 
               <td><input id="chkcc" name="chk" type="checkbox" value='.cc'/>.cc</td> 
             </tr> 
             <tr> 
               <td><input id="chkinfo" name="chk" type="checkbox" value='.info'/>.info</td> 
               <td><input id="chkmobi" name="chk" type="checkbox" value='.mobi'/>.mobi</td> 
               <td><input id="chkasia" name="chk" type="checkbox" value='.asia'/>.asia</td> 
               <td><input id="chktv" name="chk" type="checkbox" value='.tv'/>.tv</td> 
             </tr> 
             <tr> 
               <td><input id="chkorg" name="chk" type="checkbox" value='.org'/>.org</td> 
               <td><input id="chkbiz" name="chk" type="checkbox" value='.biz' />.biz</td> 
               <td><input id="chkgov" name="chk" type="checkbox" value='.gov.cn'/>.gov.cn</td> 
               <td><input id="chkorgcn" name="chk" type="checkbox" value='.org.cn'/>.org.cn</td> 
             </tr> 
             <tr> 
               <td><input id="chkcomcn" name="chk" type="checkbox" value='.com.cn'/>.com.cn</td> 
               <td><input id="chknetcn" name="chk" type="checkbox" value='.net.cn'/>.net.cn</td> 
               <td><input id="chkname" name="chk" type="checkbox" value='.name'/>.name</td> 
             </tr> 
           </table> 
       </p> 
        
       <p id="message" runat="server" style="color:red;margin-top:20px;"></p> 
     </p> 

 <p style="width:330px;margin-top:50px;margin-left:100px;">
        <p style="width:280px;margin-top:0px;">www.<input type="text" id="txtyuming" runat="server" style="width:120px;height:18px;border:1px solid #DEDEDE;"  />  <asp:Button ID="btnselect" Text="查询" runat="server" onclick="btnselect_Click" /></p>
        <p style="line-height:25px;">
            <table border="0" cellpadding="0" cellspacing="0">
             <tr>
                <td><input id="chkcom" name="chk" type="checkbox" checked="checked" value='.com' />.com</td>
                <td><input id="chknet" name="chk" type="checkbox" value='.net'/>.net</td>
                <td><input id="chkcn" name="chk" type="checkbox" value='.cn'/>.cn</td>
                <td><input id="chkcc" name="chk" type="checkbox" value='.cc'/>.cc</td>
              </tr>
              <tr>
                <td><input id="chkinfo" name="chk" type="checkbox" value='.info'/>.info</td>
                <td><input id="chkmobi" name="chk" type="checkbox" value='.mobi'/>.mobi</td>
                <td><input id="chkasia" name="chk" type="checkbox" value='.asia'/>.asia</td>
                <td><input id="chktv" name="chk" type="checkbox" value='.tv'/>.tv</td>
              </tr>
              <tr>
                <td><input id="chkorg" name="chk" type="checkbox" value='.org'/>.org</td>
                <td><input id="chkbiz" name="chk" type="checkbox" value='.biz' />.biz</td>
                <td><input id="chkgov" name="chk" type="checkbox" value='.gov.cn'/>.gov.cn</td>
                <td><input id="chkorgcn" name="chk" type="checkbox" value='.org.cn'/>.org.cn</td>
              </tr>
              <tr>
                <td><input id="chkcomcn" name="chk" type="checkbox" value='.com.cn'/>.com.cn</td>
                <td><input id="chknetcn" name="chk" type="checkbox" value='.net.cn'/>.net.cn</td>
                <td><input id="chkname" name="chk" type="checkbox" value='.name'/>.name</td>
              </tr>
            </table>
        </p>
       
        <p id="message" runat="server" style="color:red;margin-top:20px;"></p>
      </p>

 

后台代码:


[csharp]
protected void btnselect_Click(object sender, EventArgs e) 
   { 
       try 
       { 
           //检查域名是否为空  
           if (txtyuming.Value.Trim() != "") 
           { 
               string Domain = txtyuming.Value;//Domain 为要查询的域名名称  
               string Ext = ""; //Ext 为要查询的域名后缀  
 
               //获取页面中所有选中项  
               if (Request["chk"] != null) 
               { 
                   Ext = Request["chk"].ToString(); 
               } 
 
              Whois(Domain,Ext); 
           } 
       } 
       catch (Exception) 
       { } 
    
   } 

 protected void btnselect_Click(object sender, EventArgs e)
    {
        try
        {
            //检查域名是否为空
            if (txtyuming.Value.Trim() != "")
            {
                string Domain = txtyuming.Value;//Domain 为要查询的域名名称
                string Ext = ""; //Ext 为要查询的域名后缀

                //获取页面中所有选中项
                if (Request["chk"] != null)
                {
                    Ext = Request["chk"].ToString();
                }

               Whois(Domain,Ext);
            }
        }
        catch (Exception)
        { }
   
    }
[csharp] view plaincopyprint?/// <summary>  
    /// 从“中国万网(www.net.cn)”实现域名查询功能  
    /// </summary>  
    /// <param name="Domain">域名</param>  
    /// <param name="Ext">后缀</param>  
    public void Whois(string Domain, string Ext) 
    { 
        string strResult = string.Empty; 
         
        IList<string> domainList = new List<string>();//保存域名名称和后缀  
        domainList.Add(Domain);//List<string>第一元素保存域名名称,其他元素为域名后缀  
 
        //取出Ext里的后缀名放进ExtArry  
        string[] ExtArry = Ext.Split(new char[] { ',' }); 
        for (int i = 0; i < ExtArry.Length; i++) 
        { 
            domainList.Add(ExtArry[i]); 
        } 
 
        //显示域名查询情况:  
        if (domainList.Count > 1) 
        { 
            string dm = domainList[0]; 
            string dname; 
            for (int i = 1; i < domainList.Count; i++) 
            { 
                dname = dm + domainList[i]; 
                if (IsReg(dname)) 
                { 
                    strResult += dname + "可以注册!"+"<br/>"; 
                } 
                else 
                { 
                    strResult += dname + "不可以注册!" + "<br/>"; 
                } 
            } 
        } 
 
       // strResult;  
        message.InnerHtml = strResult;//输出结果;  
    } 
 
    //判断域名是否被注册(方法)  
    //利用万网的查询接口https://panda.www.net.cn/cgi-bin/check.cgi?area_domain=进行查询  
    public static bool IsReg(string domainName) 
    { 
        bool flag = false; 
        string dm = HttpContext.Current.Server.UrlEncode(domainName); 
        try 
        { 
            WebClient wc = new WebClient(); 
            string xmlstr = wc.DownloadString("https://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + dm); 
            StringReader sr = new StringReader(xmlstr); 
            XmlTextReader xr = new XmlTextReader(sr); 
            while (xr.Read()) 
            { 
                if (xr.IsStartElement("original")) 
                { 
                    xr.Read(); 
                    if (xr.Value.Substring(0, 3) == "210") 
                    { 
                        flag = true; 
                        break; 
                    } 
                    else 
                    { 
                        flag = false; 
                        break; 
                    } 
                } 
            } 
            return flag; 
        } 
        catch 
        { 
            return false; 
        } 
    } 

/// <summary>
    /// 从“中国万网(www.net.cn)”实现域名查询功能
    /// </summary>
    /// <param name="Domain">域名</param>
    /// <param name="Ext">后缀</param>
    public void Whois(string Domain, string Ext)
    {
        string strResult = string.Empty;
       
        IList<string> domainList = new List<string>();//保存域名名称和后缀
        domainList.Add(Domain);//List<string>第一元素保存域名名称,其他元素为域名后缀

        //取出Ext里的后缀名放进ExtArry
        string[] ExtArry = Ext.Split(new char[] { ',' });
        for (int i = 0; i < ExtArry.Length; i++)
        {
            domainList.Add(ExtArry[i]);
        }

        //显示域名查询情况:
        if (domainList.Count > 1)
        {
            string dm = domainList[0];
            string dname;
            for (int i = 1; i < domainList.Count; i++)
            {
                dname = dm + domainList[i];
                if (IsReg(dname))
                {
                    strResult += dname + "可以注册!"+"<br/>";
                }
                else
                {
                    strResult += dname + "不可以注册!" + "<br/>";
                }
            }
        }

       // strResult;
        message.InnerHtml = strResult;//输出结果;
    }

    //判断域名是否被注册(方法)
    //利用万网的查询接口https://panda.www.net.cn/cgi-bin/check.cgi?area_domain=进行查询
    public static bool IsReg(string domainName)
    {
        bool flag = false;
        string dm = HttpContext.Current.Server.UrlEncode(domainName);
        try
        {
            WebClient wc = new WebClient();
            string xmlstr = wc.DownloadString("https://panda.www.net.cn/cgi-bin/check.cgi?area_domain=" + dm);
            StringReader sr = new StringReader(xmlstr);
            XmlTextReader xr = new XmlTextReader(sr);
            while (xr.Read())
            {
                if (xr.IsStartElement("original"))
                {
                    xr.Read();
                    if (xr.Value.Substring(0, 3) == "210")
                    {
                        flag = true;
                        break;
                    }
                    else
                    {
                        flag = false;
                        break;
                    }
                }
            }
            return flag;
        }
        catch
        {
            return false;
        }
    }