首页 新闻 赞助 找找看

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

0
悬赏园豆:50 [已解决问题] 解决于 2011-10-31 13:47


            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的边界上跨两个循环的话这个汉字会成为乱码。大家有什么好的办法可以解决?

Hawkon的主页 Hawkon | 菜鸟二级 | 园豆:225
提问于:2011-10-26 16:34
< >
分享
最佳答案
1
StreamReader reader = new StreamReader(stream, Encoding.UTF8);

html =reader.ReadToEnd();
收获园豆:30
hbren | 小虾三级 |园豆:684 | 2011-10-30 22:07
using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text;

namespace bxg.framework.net
{
public class UrlRequest
{
public static HttpWebResponse Send(string url, string contentType, string method, CookieContainer defaultCookieContainer, Encoding encoding, string postData, out CookieContainer cookieContainer)
{
try
{
HttpWebRequest hwr;
hwr = (HttpWebRequest)WebRequest.Create(url);
if (defaultCookieContainer != null)
hwr.CookieContainer = defaultCookieContainer;
else
hwr.CookieContainer = new CookieContainer();
hwr.KeepAlive = false;
hwr.ContentLength = 0;

if (!string.IsNullOrEmpty(contentType))
hwr.ContentType = contentType;
if (!string.IsNullOrEmpty(method))
hwr.Method = method;
if (!string.IsNullOrEmpty(postData))
{
byte[] byte1 = encoding.GetBytes(postData);
hwr.ContentLength = byte1.Length;
Stream stream = hwr.GetRequestStream();
stream.Write(byte1, 0, byte1.Length);
stream.Close();
}

HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();
cookieContainer = hwr.CookieContainer;
return response;
}
catch (System.Exception e)
{
throw e;
}
}

public static HttpWebResponse Send(string url)
{
return Send(url, string.Empty, "GET", Encoding.UTF8, string.Empty);
}

public static HttpWebResponse Send(string url, string method)
{
string contentType = "";
if (method == "POST")
contentType = "application/x-www-form-urlencoded";
return Send(url, contentType, method, Encoding.UTF8, string.Empty);
}

public static HttpWebResponse Send(string url, string contentType, string method)
{
return Send(url, contentType, method, Encoding.UTF8, string.Empty);
}

public static HttpWebResponse Send(string url, string contentType, string method, Encoding encoding, string postData)
{
CookieContainer cookieContainer;
return Send(url, contentType, method, encoding, postData, out cookieContainer);
}

public static HttpWebResponse Send(string url, string contentType, string method, Encoding encoding, string postData, out CookieContainer cookieContainer)
{
return Send(url, contentType, method, null, encoding, postData, out cookieContainer);
}

public static string GetText(string url)
{
string fileBody = string.Empty;
StreamReader reader = null;
Stream stream = null;

try
{
stream = GetStream(url);
reader = new StreamReader(stream, Encoding.UTF8);
fileBody = reader.ReadToEnd();
}
finally
{
if (null != stream)
stream.Close();
}

return fileBody;
}

public static Stream GetStream(string url)
{
WebResponse response = Send(url);
Stream stream = null;
if (response != null)
stream = response.GetResponseStream();
return stream;
}

public static void Save(string file, string fileName)
{
FileStream fs = null;
try
{
fs = File.Create(fileName);
byte[] buffer;
buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(file);
fs.Write(buffer, 0, buffer.Length);
fs.Close();

}
catch (IOException e)
{
throw e;
}
finally
{
if (fs != null) fs.Close();
}
}

public static void Save(Stream file, string fileName)
{
FileStream fs = null;
try
{
fs = File.Create(fileName);
byte[] buffer = new byte[1024];

int l;
do
{
l = file.Read(buffer, 0, buffer.Length);
if (l > 0) fs.Write(buffer, 0, l);
}
while (l > 0);
}
catch (IOException e)
{
throw e;
}
finally
{
if (fs != null) fs.Close();
file.Close();
}
}
}
}
hbren | 园豆:684 (小虾三级) | 2011-10-30 22:09
其他回答(1)
0

好的方法就是用StreamReader,TextReader等流读取器替换你的Encoding.UTF8.GetString.

收获园豆:20
Launcher | 园豆:45045 (高人七级) | 2011-10-27 11:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册