首页 > 编程 > ASP > 正文

用c#写的asp+域名查询程序

2024-05-04 11:06:35
字体:
来源:转载
供稿:网友

终于有时间可以学点新东西了,今天大略看了一下有关asp+的资料,并且写了个域名查询的页面,感觉很不错,asp+比起
asp来进步实在是太大了,尽管用asp+组件也能实现域名查询的功能,并且前几天我用vc写过这么个组件,但用asp+简单方
便多了。好了,废话少提,看源码吧。

<% @page language="c#" %>
<% @assembly name="system.net" %>
<% @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);

int nlength = arrdomain[1].length ;
hashtable table = new hashtable();
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 ; //define whois server
//if the domainname's end is cn then the server is cnnic ,otherwise is networksolutions
if (arrdomain[arrdomain.length - 1] == "cn")
{
strserver = "159.226.6.139" ;
}
else
{
strserver = "whois.networksolutions.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();
if (0 == tcpc.connect(strserver, 43))
{
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();
while (-1 != sr.peek())
{
strbuilder.append(sr.readline()+"<br>");
}
tcpc.close();

bsuccess = true;
strresponse = strbuilder.tostring();
}
catch(exception e)
{
strresponse = e.tostring();
}

return bsuccess;
}
else
{
strresponse = "could not connect to whois server";
return false;
}

return false;
}
</script>
<html>
<head>
<title></title>
</head>
<body>

<form runat="server">
domain name: www . <asp:textbox id="txtdomain" value="" 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>

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表