62,254
社区成员
发帖
与我相关
我的任务
分享using (WebClient web = new WebClient())
{
data =web.DownloadString(url);
}
是个人都知道
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();
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;
}
using (WebClient web = new WebClient())
{
web.Encoding = System.Text.Encoding.UTF8;
data =web.DownloadString(url);
}