Use cookies from CookieContainer in WebBrowser

You need to make use of InternetSetCookie. Here is a sample… public partial class WebBrowserControl : Form { private String url; [DllImport(“wininet.dll”, CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData); public WebBrowserControl(String path) { this.url = path; InitializeComponent(); // set cookie InternetSetCookie(url, “JSESSIONID”, Globals.ThisDocument.sessionID); // navigate webBrowser.Navigate(url); … Read more

How can I get all Cookies of a CookieContainer?

A solution using reflection: public static CookieCollection GetAllCookies(CookieContainer cookieJar) { CookieCollection cookieCollection = new CookieCollection(); Hashtable table = (Hashtable) cookieJar.GetType().InvokeMember(“m_domainTable”, BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance, null, cookieJar, new object[] {}); foreach (var tableKey in table.Keys) { String str_tableKey = (string) tableKey; if (str_tableKey[0] == ‘.’) { str_tableKey = str_tableKey.Substring(1); } SortedList list = (SortedList) table[tableKey].GetType().InvokeMember(“m_list”, … Read more

Using CookieContainer with WebClient class

WebClient wb = new WebClient(); wb.Headers.Add(HttpRequestHeader.Cookie, “somecookie”); From Comments How do you format the name and value of the cookie in place of “somecookie” ? wb.Headers.Add(HttpRequestHeader.Cookie, “cookiename=cookievalue”); For multiple cookies: wb.Headers.Add(HttpRequestHeader.Cookie, “cookiename1=cookievalue1;” + “cookiename2=cookievalue2”);