C#读取网页拼接处乱码

loveyu 发布于 2013/09/04 12:42
阅读 154
收藏 0

在某一个地方如果是中文会有乱码的情况,是代码问题么?

public static string readHtml(Stream stream)
{
	if (stream == null) return "";
	string strBuff = "";
	byte[] bytes = new byte[256];
	int byteRead = 0;
	byteRead = stream.Read(bytes, 0, 256);
	while (byteRead != 0)
	{
		string strResp = Encoding.GetEncoding("gb2312").GetString(bytes, 0, byteRead);
		strBuff = strBuff + strResp;
		byteRead = stream.Read(bytes, 0, 256);
	}
	return strBuff;
}
加载中
0
loveyu
loveyu

我找到的解决办法,先全部读到内存流中,然后转为字符就OK了。

public static string readHtml(Stream stream)
{
	if (stream == null) return "";
	MemoryStream destination = new MemoryStream();
	stream.CopyTo(destination);
	stream.Close();
	return Encoding.GetEncoding("gb2312").GetString(destination.ToArray());
}

0
真幻的现实
真幻的现实
是coding的问题
0
mmpp33
mmpp33
gb2312 变成utf-8呢
返回顶部
顶部