学院首页>网络编程>ASP.NET>用ASP.Net编写的查询域名的程序

用ASP.Net编写的查询域名的程序

作者:飞刀 来源:www.aspcn.com 添加时间:2006-5-21 10:47:31
 这段程序最早是bigengle兄弟贴在chinaasp上的,是个很不错的程序来讲解ASP.Net的高级应用。不过最大的缺点就是查询中国域名时不支持中文,所出来的信息中文不能显示,小弟我稍微改了一下,这个已经能够支持中文了。呵呵,关于这个例子,我在国外的站点上放了了一个大家可以看

http://aspx1.brinkster.com/feidao/named.aspx

这个主机是国外的,所以对中文支持不太好(原因我在《亲密接触ASP.net》中说得很清楚),大家如果看不到中文,请用选择IE中的"编码"---"简体中文".

下面是源程序。

<% @ 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
 {
  String str;
Stream s = tcpc.GetStream();
  s.Write(arrDomain, 0, strDomain.Length);
 
  StreamReader sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
  StringBuilder strBuilder = new StringBuilder();
  while (-1 != sr.Peek())
  {
 strBuilder.Append(sr.ReadLine()+"
");
  
  }
  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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<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>
站内搜索