- 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;
- using System.Collections;
- using System.Text.RegularExpressions;
- /// <summary>
- /// SelectYuMing 的摘要说明
- /// </summary>
- public class SelectYuMing
- {
- public SelectYuMing()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- }
- /**/
- /// <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;
- }
- /// <summary>
- /// 查询域名是否存在
- /// </summary>
- /// <param name="str">所有的后缀的集合(com|cn|com.cn|hk|.net|mobi|asia|)</param>
- /// <param name="strYuMing"></param>
- /// <returns></returns>
- public ArrayList SelectYu(ArrayList str,string strYuMing)
- {
- ArrayList arr=new ArrayList();
- foreach(string strYu in str)
- {
- try
- {
- string strManage = Whois(strYuMing + strYu);
- Boolean boole = SelectURL(strManage, strYu);
- if (boole)
- {
- arr.Add("<span>此域名可以进行注册</span>");
- }
- else
- {
- arr.Add("<span style='color:red'>此域名已经注册</span>");
- }
- }catch(Exception){
- arr.Add("<span style='color:red'>网络访问失败</span>");
- }
- }
- return arr;
- }
- /// <summary>
- /// 得到服务器返回的信息 进行比较
- /// </summary>
- /// <param name="strManage">服务器返回的信息</param>
- /// <param name="houZui">根据后缀进行比较</param>
- /// <returns>域名是否可用</returns>
- protected Boolean SelectURL(string strManage, string houZui)
- {
- Boolean falge = false;
- String Pattern="";
- RegexOptions regex = new RegexOptions();
- switch (houZui)
- {
- case ".cn": //cn返回的信息
- if (strManage == "no matching record")
- {
- falge = true;
- };
- break;
- case ".com": //com返回的信息 Net返回的信息 cc返回的信息
- Pattern="No match";
- MatchCollection Matches1 = Regex.Matches(strManage, Pattern, regex);
- foreach (Match NextMatch in Matches1)
- {
- falge = true;
- }
- break;
- case ".net":
- Pattern = "No match";
- MatchCollection Matchess2 = Regex.Matches(strManage, Pattern, regex);
- foreach (Match NextMatch in Matchess2)
- {
- falge = true;
- }
- break;
- case ".tv":
- Pattern = "No match";
- MatchCollection Matchess6 = Regex.Matches(strManage, Pattern, regex);
- foreach (Match NextMatch in Matchess6)
- {
- falge = true;
- }
- break;
- case ".name":
- Pattern = "No match";
- MatchCollection Matchess7 = Regex.Matches(strManage, Pattern, regex);
- foreach (Match NextMatch in Matchess7)
- {
- falge = true;
- }
- break;
- case ".cc":
- Pattern = "No match";
- MatchCollection Matchess3 = Regex.Matches(strManage, Pattern, regex);
- foreach (Match NextMatch in Matchess3)
- {
- falge = true;
- }
- break;
- case ".mobi"://mobi返回的信息 org返回的信息 asia返回的信息
- if (strManage == "NOT FOUND/n")
- {
- falge = true;
- };
- break;
- case ".org":
- if (strManage == "NOT FOUND/n")
- {
- falge = true;
- };
- break;
- case ".info":
- if (strManage == "NOT FOUND/n")
- {
- falge = true;
- };
- break;
- case ".asia":
- if (strManage == "NOT FOUND/n")
- {
- falge = true;
- };
- break;
- case ".biz":
- Pattern = "Not found";//BIZ返回的信息
- MatchCollection Matchess4 = Regex.Matches(strManage, Pattern, regex);
- foreach (Match NextMatch in Matchess4)
- {
- falge = true;
- }
- break;
- case ".hk":
- Pattern = "Domain Not Found";//hk返回的信息
- MatchCollection Matchess5 = Regex.Matches(strManage, Pattern, regex);
- foreach (Match NextMatch in Matchess5)
- {
- falge = true;
- }
- break;
- case ".com.cn":
- if (strManage == "no matching record")
- {
- falge = true;
- }
- break;
- case ".net.cn":
- if (strManage == "no matching record")
- {
- falge = true;
- }
- break;
- }
- return falge;
- }
- }