引言
要实现域名查询首先要找到域名服务器在这用的是万网的域名服务器
网址为bin/checkcgi?area_domain=
whoisbin/whois?domain=
查询界面
输入中文域名后查询界面
判断是否为中文域名(javascript)
function IsChDomain(domainStr)
{
var flag = false;
var compStr = ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_;
var domainLength = domainStrlength;
for(var i = ;i < domainLength;i++)
{
var temp = domainStrcharCodeAt(i);
if(temp >= )//含有字符编码大于的字符判断为中文不太严格
{
flag=true;
}
else
{
var temp = compStrindexOf(domainStrcharAt(i));
if(temp == )
{
flag = false;
break;
}
}
}
return flag;
}
输入英文域名后查询界面
判断是否为英文域名
function IsEnDomain(domainStr)
{
var flag = false;
var compStr = ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_;
var domainLength = domainStrlength;
for(var i = ;i<domainLength;i++)
{
if(compStrindexOf(domainStrcharAt(i)) == )
{
flag = false;
break;
}
else
{
flag = true;
}
}
return flag;
}
中国等不能注册英文域名把相应的CheckBox的disabled属性设置为true代码为(javascript)
function Trim(domainStr)
{
return domainStrreplace(/(^s*)|(s*$)/g );
}
function checkValue()
{
if(documentgetElementById(txtDomainName)value==)
{
alert(请输入域名!);
return false;
}
if(documentgetElementById(txtDomainName)valuelength >= )
{
alert(域名长度不能超过个字符!);
return false;
}
for(var i = ;i < documentgetElementById(txtDomainName)valuelength;i++)
{
if(documentgetElementById(txtDomainName)valuecharAt(i) == )
{
alert(域名中不能含有空格!);
return false;
break;
}
}
checkDomainType();
return true;
}
function checkDomainType()
{
var domainName = documentgetElementById(txtDomainName)value;
domainName = Trim(domainName);
if(IsChDomain(domainName))
{
setCheckBox(true);
}
else if(IsEnDomain(domainName))
{
setCheckBox(false);
}
}
function setCheckBox(flag)
{
documentgetElementById(chkorg)disabled = flag;
documentgetElementById(chkorgcn)disabled = flag;
documentgetElementById(chkcomcn)disabled = flag;
documentgetElementById(chknetcn)disabled = flag;
documentgetElementById(chkgovcn)disabled = flag;
documentgetElementById(chkmobi)disabled = flag;
documentgetElementById(chkinfo)disabled = flag;
documentgetElementById(chkcn)disabled = flag;
documentgetElementById(chkchina)disabled = !flag;
documentgetElementById(chkcompany)disabled = !flag;
documentgetElementById(chknetwork)disabled = !flag;
}
查询结果界面
后台编码
例如查询域名
网址为
bin/checkcgi?area_domain=
返回的信息为XML格式
<?xml version= encoding=gb?>
<property>
<returncode></returncode>
<key></key>
<original> : Domain name is available</original>
</property>
其中表示正确返回
表示可以注册
因此可通过判断original 后的代码是否为判断域名是否可以注册
判断方法有多种处理字符串解析XML等
在这用的是解析XML方法
获取待查询的域名
string domainName = CommonClearString(txtDomainNameText );//清理字符串判断域名是否合法
if (domainName == stringEmpty)
{
lblMsgText = CommonShowMessage(请输入域名!);
return;
}
IList<string> domainList = new List<string>();//保存域名名称和后缀
domainListAdd(domainName);//List<string>第一元素保存域名名称其他元素为域名后缀
if (chkcomChecked) domainListAdd(chkcomText);
if (chknetChecked) domainListAdd(chknetText);
if (chkbizChecked) domainListAdd(chkbizText);
if (chkccChecked) domainListAdd(chkccText);
if (chktvChecked) domainListAdd(chktvText);
if (UtilityIsHasCHZN(domainName))
{
if (chkchinaChecked) domainListAdd(chkchinaText);
if (chkcompanyChecked) domainListAdd(chkcompanyText);
if (chknetworkChecked) domainListAdd(chknetworkText);
if (chkcnChecked) domainListAdd(chkcnText);
}
else
{
if (chkorgChecked) domainListAdd(chkorgText);
if (chkorgcnChecked) domainListAdd(chkorgcnText);
if (chkcomcnChecked) domainListAdd(chkcomcnText);
if (chknetcnChecked) domainListAdd(chknetcnText);
if (chkgovcnChecked) domainListAdd(chkgovcnText);
if (chkmobiChecked) domainListAdd(chkmobiText);
if (chkinfoChecked) domainListAdd(chkinfoText);
}
Session[domainName] = domainList;
ResponseRedirect(ShowDomainNameaspx);//在 ShowDomainNameaspx 中判断处理待查询的域名
判断是否为中文域名(方法)
public static bool IsHasCHZN(string domainName)
{
Regex RegCHZN = new Regex([一龥]);
Match m = RegCHZNMatch(domainName);
return mSuccess;
}
判断域名是否被注册(方法)
/// ** (C#) 实现域名查询**
///
/// File: CommonOperatecs
///
/// Author: 周振兴 (Zxjay 飘遥)
///
/// EMail:
///
/// Date:
///
/// Blog:
public static bool IsReg(string domainName)
{
bool flag = false;
string dm = HttpContextCurrentServerUrlEncode(domainName);
try
{
//判断方法非常多如打开远程文件再处理字符串等等这里用的方法效率不是很高
WebClient wc = new WebClient();
string xmlstr = wcDownloadString(bin/checkcgi?
area_domain= + dm);
StringReader sr = new StringReader(xmlstr);
XmlTextReader xr = new XmlTextReader(sr);
while (xrRead())
{
if (xrIsStartElement(original))
{
xrRead();
if (xrValueSubstring( ) == )
{
flag = true;
break;
}
else
{
flag = false;
break;
}
}
}
return flag;
}
catch
{
return false;
}
}
显示域名查询情况
if (Session[domainName] != null)
{
IList<string> il = (IList<string>)Session[domainName];
if (ilCount > )
{
string dm = il[];
string dname;
Label lbl;
for (int i = ; i < ilCount; i++)
{
dname = dm + il[i];
if (UtilityIsReg(dname))
{
lbl = new Label();
lblID = lbl + iToString();
lblText = stringFormat(<div style=padding:px; width:px;
float:left;clear:left;>{}</div> +
<div style=textalign:center;padding:px;width:px;float:left;
color:#ff;>可以注册!</div> dm + il[i]);
pnlDomainNameControlsAdd(lbl);
}
else
{
lbl = new Label();
lblID = lbl + iToString();
lblText = stringFormat(<div style=padding:px; width:px;
float:left; clear:left;><a target=_blank >{}</a></div> +
<div style=textalign:center;float:left;padding:px; width:px;>已被注册!
</div> +
<div style=float:left;padding:px;><a target=_blank
bin/whois?domain={}>查看</a></div>
dm + il[i] dm + il[i] ServerUrlEncode(dm + il[i]));
pnlDomainNameControlsAdd(lbl);
}
}
}
}
注意事项
() 编码格式必须是GB
() 域名格式判断并不太严格
() 参考了万网等的一些信息