二进制流转UTF8字符串编码时被截断

    string html;
            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.UserAgent = Comm.GetRandomUserAgent();
            var response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            byte[] data = new byte[1024];
            int count = 1024;
            StringBuilder sb = new StringBuilder();
            MemoryStream ms = new MemoryStream();
            do
            {

                count = stream.Read(data, 0, 1024);
                sb.Append(Encoding.UTF8.GetString(data, 0, count));
            } while (count >0);
            stream.Close();
            response.Close();
            html = sb.ToString();

 

问题是如果一个汉字正好在1024的边界上跨两个循环的话这个汉字会成为乱码。大家有什么好的办法可以解决?


万千封印
浏览 846回答 2
2回答

元芳怎么了

StreamReader reader = new StreamReader(stream, Encoding.UTF8); html =reader.ReadToEnd();

牧羊人nacy

好的方法就是用StreamReader,TextReader等流读取器替换你的Encoding.UTF8.GetString.
打开App,查看更多内容
随时随地看视频慕课网APP