-
C#教程之C#独立域名查询代码
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>
<% @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>
最新更新
Objective-C语法之代码块(block)的使用
VB.NET eBook
Add-in and Automation Development In VB.NET 2003 (F
Add-in and Automation Development In VB.NET 2003 (8
Add-in and Automation Development in VB.NET 2003 (6
Add-in and Automation Development In VB.NET 2003 (5
AddIn Automation Development In VB.NET 2003 (4)
AddIn And Automation Development In VB.NET 2003 (2)
Addin and Automation Development In VB.NET 2003 (3)
AddIn And Automation Development In VB.NET 2003 (1)
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
武装你的WEBAPI-OData入门
武装你的WEBAPI-OData便捷查询
武装你的WEBAPI-OData分页查询
武装你的WEBAPI-OData资源更新Delta
5. 武装你的WEBAPI-OData使用Endpoint 05-09
武装你的WEBAPI-OData之API版本管理
武装你的WEBAPI-OData常见问题
武装你的WEBAPI-OData聚合查询
OData WebAPI实践-OData与EDM
OData WebAPI实践-Non-EDM模式