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

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

1 @Page Language="C#" 
1 @Assembly Name="System.Net" 
1 @Import Namespace="System.Net.Sockets" 
1 @Import Namespace="System.Text" 
1 @Import Namespace="System.IO" 
1 @Import Namespace="System.Collections" 
 1<script language="C#" runat="server">   
 2void doQuery(Object sender, EventArgs e)   
 3{   
 4String strDomain = txtDomain.Text;   
 5char[] chSplit = {'.'};   
 6string[] arrDomain = strDomain.Split(chSplit); 
 7
 8int nLength = arrDomain[1].Length ;   
 9Hashtable table = new Hashtable();   
10table.Add("de", "whois.denic.de");   
11table.Add("be", "whois.dns.be");   
12table.Add("gov", "whois.nic.gov");   
13table.Add("mil", "whois.nic.mil"); 
14
15String strServer ; //define whois server   
16//if the domainname's end is cn then the server is cnnic ,otherwise is networksolutions   
17if (arrDomain[arrDomain.Length - 1] == "cn")   
18{   
19strServer = "159.226.6.139" ;   
20}   
21else   
22{   
23strServer = "whois.networksolutions.com";   
24} 
25
26if (table.ContainsKey(arrDomain[1]))   
27{   
28strServer = table[arrDomain][1]].ToString();   
29}   
30else if (nLength == 2)   
31{   
32// 2-letter TLD's always default to RIPE in Europe   
33strServer = "whois.ripe.net";   
34} 
35
36String strResponse;   
37bool bSuccess = DoWhoisLookup(strDomain, strServer, out strResponse);   
38if (bSuccess)   
39{   
40txtResult.Text = strResponse;   
41}   
42else   
43{   
44txtResult.Text = "Lookup failed";   
45}   
46} 
47
48bool DoWhoisLookup(String strDomain, String strServer, out String strResponse)   
49{   
50strResponse = "none";   
51bool bSuccess = false; 
52
53TCPClient tcpc = new TCPClient();   
54if (0 == tcpc.Connect(strServer, 43))   
55{   
56strDomain += "\r\n";   
57Byte[] arrDomain = Encoding.ASCII.GetBytes(strDomain.ToCharArray());   
58try   
59{   
60Stream s = tcpc.GetStream();   
61s.Write(arrDomain, 0, strDomain.Length); 
62
63StreamReader sr = new StreamReader(tcpc.GetStream(), Encoding.ASCII);   
64StringBuilder strBuilder = new StringBuilder();   
65while (-1 != sr.Peek())   
66{   
67strBuilder.Append(sr.ReadLine()+"<br>");   
68}   
69tcpc.Close(); 
70
71bSuccess = true;   
72strResponse = strBuilder.ToString();   
73}   
74catch(Exception e)   
75{   
76strResponse = e.ToString();   
77} 
78
79return bSuccess;   
80}   
81else   
82{   
83strResponse = "Could not connect to Whois server";   
84return false;   
85} 
86
87return false;   
88}   
89</script>
 1<html>
 2<head>
 3<title></title>
 4</head>
 5<body>
 6<form runat="server">   
 7Domain name: WWW . <asp:textbox id="txtDomain" runat="server" value=""></asp:textbox>
 8<asp:button id="btnQuery" onclick="doQuery" runat="server" text="Query!"></asp:button>
 9<br/><hr width="100%"/><br/>
10<asp:label id="txtResult" runat="server"></asp:label>
11</form>
12</body>
13</html>

from http://www.top888.net/zwcj/10-13/jc01.htm

Published At
Categories with Web编程
Tagged with
comments powered by Disqus