Thursday, November 6, 2014

Cookies and stuff reference C# .NET

in .net you can do like this:

[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);
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags, IntPtr dwReserved);   
const int INTERNET_COOKIE_HTTPONLY = 0x00002000;
private static CookieContainer GetUriCookieContainer(string uri)
    CookieContainer cookies = null;
    // Determine the size of the cookie
    uint datasize = 256;
    StringBuilder cookieData = new StringBuilder(256);
    if (!InternetGetCookieEx(uri, <<COOKIE_NAME_HERE>>, cookieData, ref datasize, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
        if (datasize < 0)
            return null;
        datasize = 1024;
        // Allocate stringbuilder large enough to hold the cookie
        cookieData = new StringBuilder(1024);
        if (!InternetGetCookieEx(uri, < 0)
        cookies = new CookieContainer();
        cookies.SetCookies(new Uri(uri), cookieData.ToString().Replace(';', ','));
    return cookies;