Skip to content

Instantly share code, notes, and snippets.

@SafeerH
Last active May 30, 2025 22:03
Show Gist options
  • Save SafeerH/0fe492f88f29aee265c47a1dcbe8d6c5 to your computer and use it in GitHub Desktop.
Save SafeerH/0fe492f88f29aee265c47a1dcbe8d6c5 to your computer and use it in GitHub Desktop.
Extract HttpOnly cookies (can be with WebBrowser control)
using System.Runtime.InteropServices;
public class WebHelper {
[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().Replace(';', ',');
}
else
{
return null;
}
}
}
@TSlivede
Copy link

This is still the correct Api (if you are still using an IE based browser, e.g. WebBrowser). One Pitfall: You need to specify only the base url, e.g. procoll://host.tld/ without the following path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment