利用WebClient抓取网页数据
Admin | 2016-12-26 15:48:50 | 被阅次数 | 5763
一、gb2312方式获取网页源码
public string getGB2312HTML(string url)
{
string str;
using (System.Net.WebClient client = new System.Net.WebClient())
{
using (System.IO.Stream stream = client.OpenRead(url))
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("gb2312")))
{
str = reader.ReadToEnd();
reader.Close();
}
stream.Close();
}
}
return str;
}
二、UTF-8方式获取网页源码
public string getUTF8HTML(string url)
{
string str;
using (System.Net.WebClient client = new System.Net.WebClient())
{
using (System.IO.Stream stream = client.OpenRead(url))
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, System.Text.Encoding.GetEncoding("UTF-8")))
{
str = reader.ReadToEnd();
reader.Close();
}
stream.Close();
}
}
return str;
}