public static string GetUrlData(string url,ProgressBar Prog)
{
HttpWebResponse res = null;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(http://www.baidu.com/);
res = (HttpWebResponse)req.GetResponse();
long totalBytes = res.ContentLength;
Stream input = null;
input = res.GetResponseStream();
int totalDownloadedByte = 0;
byte[] by = new byte[10245];
string Content=null;
Encoding encoder = Encoding.GetEncoding("GB2312");
do
{
totalDownloadedByte = input.Read(by, 0, (int)by.Length);
Content+=encoder.GetString(by, 0, totalDownloadedByte);
if (Prog.Value + totalDownloadedByte <= Prog.Maximum)
{
Prog.Value += totalDownloadedByte;
Application.DoEvents();
}
else
{
Prog.Value = Prog.Maximum;
}
}
while (totalDownloadedByte != 0);
}
res.Close();
return Content;
}
输出经常有一小部分错误的的文字比如??号。或者?崭窭己蟊噶?之类的。99%的文字是正常的。。要怎么写才可以100%正常?
{
HttpWebResponse res = null;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(http://www.baidu.com/);
res = (HttpWebResponse)req.GetResponse();
long totalBytes = res.ContentLength;
Stream input = null;
input = res.GetResponseStream();
int totalDownloadedByte = 0;
byte[] by = new byte[10245];
string Content=null;
Encoding encoder = Encoding.GetEncoding("GB2312");
do
{
totalDownloadedByte = input.Read(by, 0, (int)by.Length);
Content+=encoder.GetString(by, 0, totalDownloadedByte);
if (Prog.Value + totalDownloadedByte <= Prog.Maximum)
{
Prog.Value += totalDownloadedByte;
Application.DoEvents();
}
else
{
Prog.Value = Prog.Maximum;
}
}
while (totalDownloadedByte != 0);
}
res.Close();
return Content;
}
输出经常有一小部分错误的的文字比如??号。或者?崭窭己蟊噶?之类的。99%的文字是正常的。。要怎么写才可以100%正常?
解决方案 »
- 请教winform的问题,关于AcceptButton
- inno setup 汉化版的为什么加载不了皮肤?英文版是可以的
- 修改了Project属性中的Assembly name,如何在当前项目中取得这个名称?
- 如何去除DataGridView中选择记录的蓝条?
- 如何将管道结果重定向到标准输出,windows平台,命令提示符下
- 高手们帮我看看吧!关于如何使用Base64加密解密的代码!
- 关于水晶报表的错误
- 如何根据文本写反序列化
- async怎么实现非异步接口?
- 一个winform窗口,输入用户名密码后点击登陆,跳转到另外一个窗口问题
- mssql 连接 access数据库的问题
- 如何实现这个东东??
取消进度条。改成HttpWebResponse res = null;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(http://www.baidu.com/);
res = (HttpWebResponse)req.GetResponse();
Stream input = null;
input = res.GetResponseStream();
StreamReader sr = new StreamReader(input, Encoding.GetEncoding("gb2312"));
Content= sr.ReadToEnd();
res.Close();
return Content;
输出就没有错误。。
byte[] by = new byte[10245];
改成
byte[] by = new byte[90245];
还是有错误。。
越小错误就出现得越多。越大就越少。但是还是不可以完全正确
Encoding encoder = Encoding.GetEncoding("GB2312");
改成:
Encoding encoder = Encoding.GetEncoding("UTF-8");
totalDownloadedByte = input.Read(by, 0, (int)by.Length);
Content+=encoder.GetString(by, 0, totalDownloadedByte);
有没有好的写法。。
建议你把所有的数据都读完后再调用encoder.GetString()
public static string GetUrlData(string url,ProgressBar Prog)
{
HttpWebResponse res = null;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(http://www.baidu.com/);
res = (HttpWebResponse)req.GetResponse();
long totalBytes = res.ContentLength;
Stream input = null;
input = res.GetResponseStream();
int totalDownloadedByte = 0;
byte[] by = new byte[10245];
string Content=null;
Encoding encoder = Encoding.GetEncoding("GB2312");
do
{
totalDownloadedByte = input.Read(by, 0, (int)by.Length);
Content+=encoder.GetString(by, 0, totalDownloadedByte);
if (Prog.Value + totalDownloadedByte <= Prog.Maximum)
{
Prog.Value += totalDownloadedByte;
Application.DoEvents();
}
else
{
Prog.Value = Prog.Maximum;
}
}
while (totalDownloadedByte != 0);
}
res.Close();
return Content;
}
在使用gb,utf-8等编码的时候都要注意这个问题。因为encoder.getstring()是根据一个buffer来转换的。如果编码长度不定,就有可能在这个buffer的最后一个字节和下一个buffer的第一个字节是同一个字符的编码两个部分。这样encoder就不能正常的识别了。所以这种情况下要不就把所有的数据都接收的再处理,要不就自己根据编码规则自己写一个分割的函数。但是后者很麻烦的,而且执行效率很低。