Setting a cookie in a WebBrowser control

Looks like there is a better way:

Import the InternetSetCookie function:

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetSetCookie(string lpszUrlName, string lpszCookieName, string lpszCookieData);

Create the Cookie object:

Cookie temp1 = new Cookie("KEY1", "VALUE1", "/Path/To/My/App", "https://stackoverflow.com/");

Call InternetSetCookie function to set the cookie for that URL

InternetSetCookie("https://my.url.com/Path/To/My/App", null, temp1.ToString() + "; expires = Sun, 01-Jan-2013 00:00:00 GMT");

Navigate the WebBrowser to the URL you would like to go to.

webBrowser1.Navigate("https://my.url.com/Path/To/My/App");

Think this is the best solution for the issue :).

Leave a Comment