.net 有两种方式取得页面源码
1、HttpWebRequest和HttpWebResponse
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(redirectUrl); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { while (!readStream.EndOfStream) { ViewBag.Stream += readStream.ReadLine(); } }
2、WebClient
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8; string htmlStr = client.DownloadString(redirectUrl); ViewBag.Stream = htmlStr;
区别:
第一种,复杂,灵活性高。可以伪装一下进行采集或者发送登录信息。
第二种,简单,好用。遇到反采集、需要登录的话就麻烦了。
WebClient是WebRequest的再包装,简化使用,代码量少,但灵活不足.