本文实例讲述了C#使用WebClient登录网站并抓取登录后的网页信息实现方法。分享给大家供大家参考,具体如下:
C#登录网站实际上就是模拟浏览器提交表单,然后记录浏览器响应返回的会话Cookie值,再次发送请求时带着这个会话cookie值去请求就可以实现模拟登录的效果了。
如下类CookieAwareWebClient实现在发送请求时都带着cookie。
public class CookieAwareWebClient : WebClient{ private CookieContainer cookie = new CookieContainer(); protected override WebRequest GetWebRequest(Uri address) { WebRequest request = base.GetWebRequest(address); if (request is HttpWebRequest) { (request as HttpWebRequest).CookieContainer = cookie; } return request; }}
如下是模拟表单提交登录的使用示例:
var client = new CookieAwareWebClient();client.BaseAddress = @"https://hovertree.net/any/base/url/";var loginData = new NameValueCollection();loginData.Add("login", "YourLogin");loginData.Add("password", "YourPassword");client.UploadValues("login.php", "POST", loginData);//Now you are logged in and can request pagesstring htmlSource = client.DownloadString("index.php");
希望本文所述对大家C#程序设计有所帮助。
新闻热点
疑难解答