当前位置: 编程技术>.net/c#/asp.net
C#独立域名查询代码
来源: 互联网 发布时间:2014-10-13
本文导语: whois.aspx 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) ...
whois.aspx
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 += "rn";
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+"
");
}
tcpc.Close();
bSuccess = true;
strResponse = strBuilder.ToString();
}
catch(Exception e)
{
strResponse = e.ToString();
}
return bSuccess;
}
域名whois查询(.NET版):
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 += "rn";
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+"
");
}
tcpc.Close();
bSuccess = true;
strResponse = strBuilder.ToString();
}
catch(Exception e)
{
strResponse = e.ToString();
}
return bSuccess;
}
域名whois查询(.NET版):
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。