How do I copy all cookies from a WebBrowser class and put them in a CookieCollection?
The title pretty much stands for itself. But I am using an HTTPWebRequest to download pages from the internet, but these p开发者_StackOverflowages require a login, so you log in with the WebBrowser and it uses the resulting cookies to be able to download the actual page.
HttpWebRequest request = (HttpWebRequest) WebRequest.Create (myUri);
request.CookieContainer = new CookieContainer ();
request.CookieContainer.SetCookies (myUri, webBrowser1.Document.Cookie);
Edit: For your case, you may consider a NetworkCredential Object. It will work perfectly without any need for a WebBrowser or Cookies.
WebRequest myWebRequest = WebRequest.Create(url);
NetworkCredential networkCredential = new NetworkCredential(username, password);
myWebRequest.Credentials = networkCredential;
精彩评论