Get Cookies from WebBrowser controls in Visual C#

This code get simple cookie and get also HTTP only cookie.

add this namespace
using System.Runtime.InteropServices;

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref uint pcchCookieData, int dwFlags, IntPtr lpReserved);
const int INTERNET_COOKIE_HTTPONLY = 0x00002000;
public static string GetGlobalCookies(string uri)
{
uint datasize = 1024;
StringBuilder cookieData = new StringBuilder((int)datasize);
if (InternetGetCookieEx(uri, null, cookieData, ref datasize, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero)
&& cookieData.Length > 0)
{
return cookieData.ToString();
}
else
{
return null;
}
}
httpWebRequest.CookieContainer.SetCookies(
webBrowser.Document.Url,
GetGlobalCookies(webBrowser.Document.Url.AbsoluteUri)
);

See Also An important place to find a huge amount of Visual C# related articles is the TechNet Wiki itself. The best entry point is Visual C# Resources on the TechNet Wiki