downloadString 乱码

sohighthesky 2010-01-22 12:11:35
using (WebClient web = new WebClient())
{
data =web.DownloadString(url);
}


这个当url为http://www.baidu.com是就是好的,但是http://www.cnblogs.com/却是乱码
有没办法解决?
想得到是网页的源代码,或者不用WebClient 这个类也可以
...全文
785 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
崔鹏飞 2010-12-12
  • 打赏
  • 举报
回复
good
thanks
wuyq11 2010-01-22
  • 打赏
  • 举报
回复
WebClient client = new WebClient();
string s = client.DownloadString("http://www.cnblogs.com");
Console.WriteLine(Encoding.UTF8.GetString(Encoding.Default.GetBytes(s)));
获取网页字符编码
Match m= Regex.Match(s, "<meta([^<]*)charset=([^<]*)\"", RegexOptions.IgnoreCase | RegexOptions.Multiline);
string CharSet = m.Groups[2].Value;
sohighthesky 2010-01-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 tianzhichengzhr 的回复:]
乱码一般都是编码不同引起的
[/Quote]是个人都知道
sohighthesky 2010-01-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wzp144650 的回复:]
为什么不用webrequset呀?

[/Quote]
webrequest怎么写?
wzp144650 2010-01-22
  • 打赏
  • 举报
回复
为什么不用webrequset呀?
tzc 2010-01-22
  • 打赏
  • 举报
回复
乱码一般都是编码不同引起的
mngzilin 2010-01-22
  • 打赏
  • 举报
回复
用webbrower最好,能够自动识别编码,

webrequest webclient都需要设置编码,但是网站编码不是统一的,所以有的会有乱码。
happy664618843 2010-01-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wzp144650 的回复:]
C# code

WebRequest hwr= WebRequest.Create(@"http://www.cnblogs.com") ;
HttpWebResponse hwp= hwr.GetResponse()as HttpWebResponse;string text;
StreamReader sr;string code= hwp.ContentType;//得到编码了 code= code.Split('=')[1];
Stream rep= hwp.GetResponseStream();switch (code)
{case"utf-8":
sr=new StreamReader(rep, Encoding.UTF8);
text= sr.ReadToEnd();break;case"gb2312":
sr=new StreamReader(rep, Encoding.GetEncoding("gb2312"));
text= sr.ReadToEnd();break;default:break;
}
[/Quote]
up
wzp144650 2010-01-22
  • 打赏
  • 举报
回复
改一下。。。不应该用switch的。。。

WebRequest hwr = WebRequest.Create(@"http://www.baidu.com") ;
HttpWebResponse hwp = hwr.GetResponse() as HttpWebResponse;
string text;
StreamReader sr;
string code = hwp.ContentType;
//得到编码了
code = code.Split('=')[1];
Stream rep = hwp.GetResponseStream();
sr = new StreamReader(rep, Encoding.GetEncoding(code));
text = sr.ReadToEnd();
wzp144650 2010-01-22
  • 打赏
  • 举报
回复


WebRequest hwr = WebRequest.Create(@"http://www.cnblogs.com") ;
HttpWebResponse hwp = hwr.GetResponse() as HttpWebResponse;
string text;
StreamReader sr;
string code = hwp.ContentType;
//得到编码了
code = code.Split('=')[1];
Stream rep = hwp.GetResponseStream();
switch (code)
{
case "utf-8":
sr= new StreamReader(rep, Encoding.UTF8);
text = sr.ReadToEnd();
break;
case "gb2312":
sr = new StreamReader(rep, Encoding.GetEncoding("gb2312"));
text = sr.ReadToEnd();
break;
default:
break;
}
watsonchia 2010-01-22
  • 打赏
  • 举报
回复 1

using (WebClient web = new WebClient())
{
web.Encoding = System.Text.Encoding.UTF8;
data =web.DownloadString(url);
}

62,254

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧