Whois.cs文件如下:
--------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Net.Sockets;
using System.IO;namespace tstWhois.LeeCen.Whois
{
class Whois
{
//将要查询的域名
private string _DomainName;
public string DomainName
{
get { return _DomainName; }
set
{
if (value.ToLower().IndexOf("www.") >= 0)
{
_DomainName = value.Substring(4, value.Length - 4);
}
else
{
_DomainName = value;
}
}
} //将要查询的Whois服务器地址
private string _WhoisServer;
public string WhoisServer
{
get { return _WhoisServer; }
set
{
if (value.EndsWith(".cn"))
{
_WhoisServer = value;
}
else
{
Hashtable table = this.GetWhoisServers();
string domainType = this.GetDomainType();
_WhoisServer = table[domainType.ToString()].ToString();
}
}
} //默认端口是43
public int Port = 43; //构造函数
public Whois()
{
this.DomainName = "59.cn";
this.WhoisServer = "whois.cnnic.net.cn";
} public Whois(string strDomainName)
{
this.DomainName = strDomainName;
this.WhoisServer = "whois.cnnic.net.cn";
} public Whois(string strDomainName, string strWhoisServer)
{
this.DomainName = strDomainName;
this.WhoisServer = strWhoisServer;
} //得到默认的Whois服务器信息
public Hashtable GetWhoisServers()
{
Hashtable serTable = new Hashtable();
serTable.Add("cn", "whois.cnnic.net.cn"); //一般cn域名的whois
serTable.Add("com.cn", "whois.cnnic.net.cn");
serTable.Add("org.cn", "whois.cnnic.net.cn");
serTable.Add("net.cn", "whois.cnnic.net.cn");
serTable.Add("info.cn", "whois.cnnic.net.cn");
serTable.Add("biz.cn", "whois.cnnic.net.cn");
serTable.Add("edu.cn", "whois.edu.cn"); //教育网
serTable.Add("com", "whois.internic.net"); //在美国的(开始)
serTable.Add("net", "whois.internic.net"); //
serTable.Add("org", "whois.internic.net"); //
serTable.Add("biz", "whois.internic.net"); //
serTable.Add("info", "whois.internic.net"); //
serTable.Add("name", "whois.internic.net"); //在美国的(结束)
serTable.Add("tw", "whois.twnic.net"); //台湾(tw)的域名
serTable.Add("jp", "whois.nic.ad.jp"); //是本的
serTable.Add("kr", "whois.krnic.net"); //韩国的
return serTable;
} //得到后缀名,即是cn或是com之类
public string GetDomainType()
{
string domainType = "";
if (this.DomainName.IndexOf(".") >= 0)
{
string[] domain = this.DomainName.Split(new Char[] { '.' });
domainType = domain[domain.Length - 1];
if (domain.Length == 3)
{
domainType = domain[1] + "." + domainType;
}
return domainType;
}
else
return "error";
} //得到完整域名
public string GetFullDomainName()
{
return "www." + this.DomainName;
} //得到TcpClient
private TcpClient GetWhoisTcpClient()
{
TcpClient tcp = new TcpClient();
tcp.Connect(this.WhoisServer, this.Port);
return tcp;
}
//重载
private TcpClient GetWhoisTcpClient(string strWhoisServer, int intPort)
{
TcpClient tcp = new TcpClient();
tcp.Connect(strWhoisServer, intPort);
return tcp;
} //得到输出流
] public string GetWhoisResponse()
{
TcpClient tcp = this.GetWhoisTcpClient();
Stream stream = tcp.GetStream(); string inDomainName,line;
inDomainName = this.DomainName + "\r\n"; byte[] inRequest = Encoding.ASCII.GetBytes(inDomainName.ToCharArray());
stream.Write(inRequest, 0, inRequest.Length);
StreamReader reader = new StreamReader(tcp.GetStream(), Encoding.GetEncoding("gb2312"));
StringBuilder outResponse = new StringBuilder(); while ((line = reader.ReadLine()) != null)
{
outResponse.Append(line+"\r\n");
}
stream.Close();
reader.Close();
tcp.Close();
return outResponse.ToString();
}
}
}
--------------------------------------------
调用部分:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Net.Sockets;
using System.IO;namespace tstWhois
{
class Program
{
static void Main(string[] args)
{
LeeCen.Whois.Whois who = new LeeCen.Whois.Whois("baidu.cn"); Console.WriteLine("要探测的域名:" + who.DomainName+",查询DNS服务器是"+who.WhoisServer+"\n------------------------------------------------------");
foreach (DictionaryEntry key in who.GetWhoisServers())
{
Console.WriteLine("Key = " + key.Key.ToString() + "\tValue" + key.Value.ToString());
}
Console.WriteLine("------------------------------------------------------\n后缀是" + who.GetDomainType());
Console.WriteLine("查询信息:\n" + who.GetWhoisResponse()); /*EncodingInfo[] code = Encoding.GetEncodings();
for (int i = 0; i < code.Length; i++)
{
Console.WriteLine("CodePage:"+code[i].CodePage+"\tDisplayName:"+code[i].DisplayName+"\tName;"+code[i].Name);
}
*/
Console.Read();
}
}
}
-----------------------------
输出结果,英文可以显示(本来就是),但是中文是乱码,不知是什么原因,请各位专家帮忙.
--------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Net.Sockets;
using System.IO;namespace tstWhois.LeeCen.Whois
{
class Whois
{
//将要查询的域名
private string _DomainName;
public string DomainName
{
get { return _DomainName; }
set
{
if (value.ToLower().IndexOf("www.") >= 0)
{
_DomainName = value.Substring(4, value.Length - 4);
}
else
{
_DomainName = value;
}
}
} //将要查询的Whois服务器地址
private string _WhoisServer;
public string WhoisServer
{
get { return _WhoisServer; }
set
{
if (value.EndsWith(".cn"))
{
_WhoisServer = value;
}
else
{
Hashtable table = this.GetWhoisServers();
string domainType = this.GetDomainType();
_WhoisServer = table[domainType.ToString()].ToString();
}
}
} //默认端口是43
public int Port = 43; //构造函数
public Whois()
{
this.DomainName = "59.cn";
this.WhoisServer = "whois.cnnic.net.cn";
} public Whois(string strDomainName)
{
this.DomainName = strDomainName;
this.WhoisServer = "whois.cnnic.net.cn";
} public Whois(string strDomainName, string strWhoisServer)
{
this.DomainName = strDomainName;
this.WhoisServer = strWhoisServer;
} //得到默认的Whois服务器信息
public Hashtable GetWhoisServers()
{
Hashtable serTable = new Hashtable();
serTable.Add("cn", "whois.cnnic.net.cn"); //一般cn域名的whois
serTable.Add("com.cn", "whois.cnnic.net.cn");
serTable.Add("org.cn", "whois.cnnic.net.cn");
serTable.Add("net.cn", "whois.cnnic.net.cn");
serTable.Add("info.cn", "whois.cnnic.net.cn");
serTable.Add("biz.cn", "whois.cnnic.net.cn");
serTable.Add("edu.cn", "whois.edu.cn"); //教育网
serTable.Add("com", "whois.internic.net"); //在美国的(开始)
serTable.Add("net", "whois.internic.net"); //
serTable.Add("org", "whois.internic.net"); //
serTable.Add("biz", "whois.internic.net"); //
serTable.Add("info", "whois.internic.net"); //
serTable.Add("name", "whois.internic.net"); //在美国的(结束)
serTable.Add("tw", "whois.twnic.net"); //台湾(tw)的域名
serTable.Add("jp", "whois.nic.ad.jp"); //是本的
serTable.Add("kr", "whois.krnic.net"); //韩国的
return serTable;
} //得到后缀名,即是cn或是com之类
public string GetDomainType()
{
string domainType = "";
if (this.DomainName.IndexOf(".") >= 0)
{
string[] domain = this.DomainName.Split(new Char[] { '.' });
domainType = domain[domain.Length - 1];
if (domain.Length == 3)
{
domainType = domain[1] + "." + domainType;
}
return domainType;
}
else
return "error";
} //得到完整域名
public string GetFullDomainName()
{
return "www." + this.DomainName;
} //得到TcpClient
private TcpClient GetWhoisTcpClient()
{
TcpClient tcp = new TcpClient();
tcp.Connect(this.WhoisServer, this.Port);
return tcp;
}
//重载
private TcpClient GetWhoisTcpClient(string strWhoisServer, int intPort)
{
TcpClient tcp = new TcpClient();
tcp.Connect(strWhoisServer, intPort);
return tcp;
} //得到输出流
] public string GetWhoisResponse()
{
TcpClient tcp = this.GetWhoisTcpClient();
Stream stream = tcp.GetStream(); string inDomainName,line;
inDomainName = this.DomainName + "\r\n"; byte[] inRequest = Encoding.ASCII.GetBytes(inDomainName.ToCharArray());
stream.Write(inRequest, 0, inRequest.Length);
StreamReader reader = new StreamReader(tcp.GetStream(), Encoding.GetEncoding("gb2312"));
StringBuilder outResponse = new StringBuilder(); while ((line = reader.ReadLine()) != null)
{
outResponse.Append(line+"\r\n");
}
stream.Close();
reader.Close();
tcp.Close();
return outResponse.ToString();
}
}
}
--------------------------------------------
调用部分:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Net.Sockets;
using System.IO;namespace tstWhois
{
class Program
{
static void Main(string[] args)
{
LeeCen.Whois.Whois who = new LeeCen.Whois.Whois("baidu.cn"); Console.WriteLine("要探测的域名:" + who.DomainName+",查询DNS服务器是"+who.WhoisServer+"\n------------------------------------------------------");
foreach (DictionaryEntry key in who.GetWhoisServers())
{
Console.WriteLine("Key = " + key.Key.ToString() + "\tValue" + key.Value.ToString());
}
Console.WriteLine("------------------------------------------------------\n后缀是" + who.GetDomainType());
Console.WriteLine("查询信息:\n" + who.GetWhoisResponse()); /*EncodingInfo[] code = Encoding.GetEncodings();
for (int i = 0; i < code.Length; i++)
{
Console.WriteLine("CodePage:"+code[i].CodePage+"\tDisplayName:"+code[i].DisplayName+"\tName;"+code[i].Name);
}
*/
Console.Read();
}
}
}
-----------------------------
输出结果,英文可以显示(本来就是),但是中文是乱码,不知是什么原因,请各位专家帮忙.
解决方案 »
- c# 通过webbrowser控件绑定httpwatch(一个BHO),运行出现错误,如何解决?
- 执行下面代码结果不是自己想的:预想的是生成一个Y20100525000002.DBF;结果生成的是Y2010052.DBF!
- GDI 画图的问题,高手来帮忙( 关于给图加事件)
- 在Datagridview中定义rowheadermouseclick事件
- 父类对象访问子类方法的问题
- access数据库 一个表 设置两个自增长字段 怎么弄 我知道只可以设置一个 但是有什么解决办法吗?
- 高手帮忙啊,郁闷了一个晚上,在线等……
- 请教过来的高手指点指点学习方法,C#,ASP.NET,SQL SERVER学习时的方法问题
- 年前的帖子都结了吧.别压到07年的年底了
- 我刚开始学C#,有个很菜的问题,不好意思,别笑话我。
- 开发定时工作
- DropDownList的一个小问题
------------------------------------
才开始玩csdn,我咋把分加给你!
你可以点管理帖子,然后再得飞那个地方会变成可编辑的。
对没楼输入完了你想给的分后点结贴。
注:结贴需要密码
用Encoding.Default把当前系统默认字体调出来应该比较好。------------我试过了,不行的,utf-8行.回cnfixit :
csdn
玩?
---------------是的.几年前学asp时就是在论坛上泡着学成的,163的,后来转行搞文字工作就没怎么泡过论坛,现在学c#,就来csdn了.
walkingmu 5.谢谢,以后还希望多多指导!