开发者社区> 问答> 正文

C#读取网页拼接处乱码:报错

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

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;
}

展开
收起
kun坤 2020-06-07 17:13:09 469 0
1 条回答
写回答
取消 提交回答
  • 我找到的解决办法,先全部读到内存流中,然后转为字符就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());
    }

    ######是coding的问题###### gb2312 变成utf-8呢
    2020-06-07 17:13:14
    赞同 展开评论 打赏
问答分类:
C#
问答标签:
问答地址:
相关产品:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载