首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#中的乱码有关问题,寻求解答

2012-03-29 
C#中的乱码问题,寻求解答Whois.cs文件如下:--------------------------using Systemusing System.Collect

C#中的乱码问题,寻求解答
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;
  }

  //得到输出流
][b] 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();
  }[/b]
  }
}
--------------------------------------------
调用部分:
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();


  }
  }
}


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

[解决办法]
换一个编码,换成utf-8的试试看,写入流的文件编码也用utf-8的试试看
[解决办法]
回arserangel:
你可以点管理帖子,然后再得飞那个地方会变成可编辑的。
对没楼输入完了你想给的分后点结贴。
注:结贴需要密码

热点排行