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();
        }
    }
}
-----------------------------
输出结果,英文可以显示(本来就是),但是中文是乱码,不知是什么原因,请各位专家帮忙.

解决方案 »

  1.   

    换一个编码,换成utf-8的试试看,写入流的文件编码也用utf-8的试试看
      

  2.   

    我晕.终于成了.前后两个必须都是utf8才行.
      

  3.   

    Fibona
    ------------------------------------
    才开始玩csdn,我咋把分加给你!
      

  4.   

    用Encoding.Default把当前系统默认字体调出来应该比较好。
      

  5.   

    回arserangel:
    你可以点管理帖子,然后再得飞那个地方会变成可编辑的。
    对没楼输入完了你想给的分后点结贴。
    注:结贴需要密码
      

  6.   

    回walkingmu :
    用Encoding.Default把当前系统默认字体调出来应该比较好。------------我试过了,不行的,utf-8行.回cnfixit :
    csdn 
    玩?
    ---------------是的.几年前学asp时就是在论坛上泡着学成的,163的,后来转行搞文字工作就没怎么泡过论坛,现在学c#,就来csdn了.
      

  7.   

    Fibona  25,
    walkingmu 5.谢谢,以后还希望多多指导!