C # WebRequest using WebBrowser cookie

I enter the site using WebBrowser, then I want to use regex to get some data, but webRequest did not use the Browse web cookie.

my webBrowser is public, is there a way to use the WebBrowser cookie in webRequest?

+9
c # browser webrequest
Mar 16 '09 at 13:59
source share
3 answers
public CookieContainer GetCookieContainer() { CookieContainer container = new CookieContainer(); foreach (string cookie in webBrowser1.Document.Cookie.Split(';')) { string name = cookie.Split('=')[0]; string value = cookie.Substring(name.Length + 1); string path = "/"; string domain = ".google.com"; //change to your domain name container.Add(new Cookie(name.Trim(), value.Trim(), path, domain)); } return container; } 

This will work on most sites, however sites using subdomains can be a problem.

+12
May 11 '10 at 18:34
source share

You can use CookieContainer for Webrequest.

  web_cookies = new CookieContainer(); // Create a 'WebRequest' object with the specified url. HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url); myWebRequest.CookieContainer = web_cookies; 

Hope this helps.

Ok, you want to log in. This is a completely different story. You can use NetworkCredential for this.

 public string get_secure_webpage(string url, string username, string password) { WebRequest myWebRequest = WebRequest.Create(url); NetworkCredential networkCredential = new NetworkCredential(username, password); myWebRequest.Credentials = networkCredential; 

...

+7
Mar 16 '09 at 14:15
source share

Is it silver light? if so, since silverlight 3, if you use the browserโ€™s network stack, you should receive cookies for free. By default, you get the browser stack when you create n HttpWebrequest using the WebRequest.Create () method. note that if you use the CreateHTTP method, you get a client stack that does not include browser cookies by default (you need to cheat in order to receive them as described earlier)

see http://msdn.microsoft.com/en-us/library/dd920295(VS.95).aspx for network stacks in silverlight since version 3

0
Oct 20 '10 at 22:05
source share



All Articles