c#获取网页内容的三种方法

1.webclient

WebClient wc = new WebClient();
Stream stm = wc.OpenRead(str_url);
StreamReader sr = new StreamReader(stm,Encoding.UTF8);
//Encoding这个类的属性有:UTF8,Unicode等等
Console.Write(sr.ReadToEnd());

2.

HttpWebRequest hwrq = (HttpWebRequest)WebRequest.Create(str_url);
HttpWebResponse hwrp = (HttpWebResponse)hwrq.GetResponse();
StreamReader sr = new StreamReader(hwrp.GetResponseStream(),Encoding.UTF8);
Console.Write(sr.ReadToEnd()); Stream st = WebRequest.Create(str_url).GetResponse().GetResponseStream();
StreamReader str = new StreamReader (st,Encoding.UTF8);
Console .Write(str.ReadToEnd ());

3.

string str_url = "http://www.baidu.com/";
WebClient wclient = new WebClient();
string str_content = wclient.DownloadString(str_url);
byte[] bt_content = new byte[str_content.Length];
bt_content = Encoding.Default.GetBytes(str_content); //此处不可吧Default转换成UTF8,否则造成转了两次,就乱码了。
str_content = Encoding.UTF8.GetString(bt_content);
Console.WriteLine(str_content);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
上一篇:MySQL Study之--Mysql无法启动“mysql.host”


下一篇:JS 时间格式化函数