C#独立域名查询
2024-07-21 02:19:18
供稿:网友
菜鸟学堂:
个人收藏.出自国外高手
whois.aspx
<% @page language="c#" %>
<% @import namespace="system.net.sockets" %>
<% @import namespace="system.text" %>
<% @import namespace="system.io" %>
<% @import namespace="system.collections" %>
<script language="c#" runat="server">
void doquery(object sender, eventargs e)
{
string strdomain = txtdomain.text;
char[] chsplit = {'.'};
string[] arrdomain = strdomain.split(chsplit);
// es darf genau ein domain name + ein suffix sein
if (arrdomain.length != 2)
{
return;
}
// das suffic darf nur 2 oder 3 zeichen lang sein
int nlength = arrdomain[1].length;
if (nlength != 2 && nlength != 3)
{
return;
}
hashtable table = new hashtable();
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");
string strserver = "whois.onlinenic.com";
if (table.containskey(arrdomain[1]))
{
strserver = table[arrdomain[1]].tostring();
}
else if (nlength == 2)
{
// 2-letter tld's always default to ripe in europe
strserver = "whois.ripe.net";
}
string strresponse;
bool bsuccess = dowhoislookup(strdomain, strserver, out strresponse);
if (bsuccess)
{
txtresult.text = strresponse;
}
else
{
txtresult.text = "lookup failed";
}
}
bool dowhoislookup(string strdomain, string strserver, out string strresponse)
{
strresponse = "none";
bool bsuccess = false;
tcpclient tcpc = new tcpclient();
try
{
tcpc.connect(strserver, 43);
}
catch(socketexception ex)
{
strresponse = "could not connect to whois server";
return false;
}
strdomain += "/r/n";
byte[] arrdomain = encoding.ascii.getbytes(strdomain.tochararray());
try
{
stream s = tcpc.getstream();
s.write(arrdomain, 0, strdomain.length);
streamreader sr = new streamreader(tcpc.getstream(), encoding.ascii);
stringbuilder strbuilder = new stringbuilder();
string strline = null;
while (null != (strline = sr.readline()))
{
strbuilder.append(strline+"<br>");
}
tcpc.close();
bsuccess = true;
strresponse = strbuilder.tostring();
}
catch(exception e)
{
strresponse = e.tostring();
}
return bsuccess;
}
</script>
<html>
<head>
<title></title>
</head>
<body>
<form runat="server">
域名whois查询(.net版): <asp:textbox id="txtdomain" value="3cts.com" runat="server" />
<asp:button id="btnquery" onclick="doquery" text="query!" runat="server" />
<br><hr width="100%"><br>
<asp:label id="txtresult" runat="server" />
</form>
</body>
</html>