您的位置:首页 > Web前端 > HTML

利用WebClient进行数据抓取

2011-08-15 13:42 225 查看
//2011-08-15 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;
}

//2011-08-15 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string stream url html