protected void btn_click(Object sender, EventArgs e)
{
String strServer;
String strDomain = txtDomain.Text;
String strServerCom = "whois.networksolutions.com";
String strServerCN = "whois.cnnic.net.cn";
String strResponse;
string[] arrDomain = strDomain.Split('.');
if (arrDomain[1].ToUpper() == "CN")
{
strServer = strServerCN;
}
else
{
strServer = strServerCom;
}
bool blSuccess = IsWhosisSuccess(strDomain, strServer, out strResponse);
if (blSuccess)
{
txtResult.Text = strResponse;
}
else
{
txtResult.Text = "查找失败.";
}
}
bool IsWhosisSuccess(String strDomain, String strServer,out String strResponse)
{
strResponse = "none";
bool blSuccess = false;
TcpClient tcpc = new TcpClient();
try
{
tcpc.Connect(strServer, 43);
}
catch (SocketException ex)
{
strResponse = "连接不到该 Whois server,请稍后再试。";
return false;
}
strDomain += "\r\n";
Byte[] arrDomain = Encoding.UTF8.GetBytes(strDomain.ToCharArray());
try
{
Stream s = tcpc.GetStream();
s.Write(arrDomain, 0, strDomain.Length);
StreamReader sr = new StreamReader(tcpc.GetStream(), Encoding.UTF8);
StringBuilder strBuilder = new StringBuilder();
string strLine = null;
while (null != (strLine = sr.ReadLine()))
{
strBuilder.Append(strLine + "<br>");
}
tcpc.Close();
blSuccess = true;
strResponse = strBuilder.ToString();
}
catch (Exception e)
{
strResponse = e.ToString();
}
return blSuccess;
}
{
String strServer;
String strDomain = txtDomain.Text;
String strServerCom = "whois.networksolutions.com";
String strServerCN = "whois.cnnic.net.cn";
String strResponse;
string[] arrDomain = strDomain.Split('.');
if (arrDomain[1].ToUpper() == "CN")
{
strServer = strServerCN;
}
else
{
strServer = strServerCom;
}
bool blSuccess = IsWhosisSuccess(strDomain, strServer, out strResponse);
if (blSuccess)
{
txtResult.Text = strResponse;
}
else
{
txtResult.Text = "查找失败.";
}
}
bool IsWhosisSuccess(String strDomain, String strServer,out String strResponse)
{
strResponse = "none";
bool blSuccess = false;
TcpClient tcpc = new TcpClient();
try
{
tcpc.Connect(strServer, 43);
}
catch (SocketException ex)
{
strResponse = "连接不到该 Whois server,请稍后再试。";
return false;
}
strDomain += "\r\n";
Byte[] arrDomain = Encoding.UTF8.GetBytes(strDomain.ToCharArray());
try
{
Stream s = tcpc.GetStream();
s.Write(arrDomain, 0, strDomain.Length);
StreamReader sr = new StreamReader(tcpc.GetStream(), Encoding.UTF8);
StringBuilder strBuilder = new StringBuilder();
string strLine = null;
while (null != (strLine = sr.ReadLine()))
{
strBuilder.Append(strLine + "<br>");
}
tcpc.Close();
blSuccess = true;
strResponse = strBuilder.ToString();
}
catch (Exception e)
{
strResponse = e.ToString();
}
return blSuccess;
}