当前位置: 移动技术网 > IT编程>开发语言>c# > C#独立域名查询代码

C#独立域名查询代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
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> 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网