whois.aspx
<%@PageLanguage="C#"%>
<%@ImportNamespace="System.Net.Sockets"%>
<%@ImportNamespace="System.Text"%>
<%@ImportNamespace="System.IO"%>
<%@ImportNamespace="System.Collections"%>
<scriptlanguage="C#"runat="server">
voiddoQuery(Objectsender,EventArgse)
{
StringstrDomain=txtDomain.Text;
char[]chSplit={'.'};
string[]arrDomain=strDomain.Split(chSplit);
//esdarfgenaueindomainname+einsuffixsein
if(arrDomain.Length!=2)
{
return;
}
//dassufficdarfnur2oder3zeichenlangsein
intnLength=arrDomain[1].Length;
if(nLength!=2&&nLength!=3)
{
return;
}
Hashtabletable=newHashtable();
table.Add("at","whois.nic.at");
table.Add("de","whois.denic.de");
table.Add("be","whois.dns.be");
table.Add("gov","whois.nic.gov");
table.Add("mil","whois.nic.mil");
StringstrServer="whois.OnlineNIC.com";
if(table.ContainsKey(arrDomain[1]))
{
strServer=table[arrDomain[1]].ToString();
}
elseif(nLength==2)
{
//2-letterTLD'salwaysdefaulttoRIPEinEurope
strServer="whois.ripe.net";
}
StringstrResponse;
boolbSuccess=DoWhoisLookup(strDomain,strServer,outstrResponse);
if(bSuccess)
{
txtResult.Text=strResponse;
}
else
{
txtResult.Text="Lookupfailed";
}
}
boolDoWhoisLookup(StringstrDomain,StringstrServer,outStringstrResponse)
{
strResponse="none";
boolbSuccess=false;
TcpClienttcpc=newTcpClient();
try
{
tcpc.Connect(strServer,43);
}
catch(SocketExceptionex)
{
strResponse="CouldnotconnecttoWhoisserver";
returnfalse;
}
strDomain+="rn";
Byte[]arrDomain=Encoding.ASCII.GetBytes(strDomain.ToCharArray());
try
{
Streams=tcpc.GetStream();
s.Write(arrDomain,0,strDomain.Length);
StreamReadersr=newStreamReader(tcpc.GetStream(),Encoding.ASCII);
StringBuilderstrBuilder=newStringBuilder();
stringstrLine=null;
while(null!=(strLine=sr.ReadLine()))
{
strBuilder.Append(strLine+"<br>");
}
tcpc.Close();
bSuccess=true;
strResponse=strBuilder.ToString();
}
catch(Exceptione)
{
strResponse=e.ToString();
}
returnbSuccess;
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<formrunat="server">
域名whois查询(.NET版):<asp:TextBoxid="txtDomain"value="3cts.com"runat="server"/>
<asp:Buttonid="btnQuery"OnClick="doQuery"text="Query!"runat="server"/>
<BR><HRwidth="100%"><BR>
<asp:labelid="txtResult"runat="server"/>
</form>
</body>
</html>