Thursday, November 6, 2014

Cookies and stuff reference C# .NET



http://stackoverflow.com/questions/18667931/httpwebrequest-add-cookie-to-cookiecontainer-argumentexception-parameternam
https://parse.com/questions/curl-and-c
http://stackoverflow.com/questions/4248672/httpwebrequest-and-set-cookie-header-in-response-not-parsed-wp7
http://www.codeproject.com/Questions/773849/HttpWebRequest-always-returns-but-works-great-with
http://stackoverflow.com/questions/16465625/get-httponly-cookies-using-httpwebrequest
http://stackoverflow.com/questions/3062925/c-sharp-get-httponly-cookie
http://www.codewrecks.com/blog/index.php/2011/04/12/use-a-webbrowser-to-login-into-a-site-that-use-httponly-cookie/
http://www.codeproject.com/Articles/38616/Retrieve-HttpOnly-Session-Cookie-in-WebBrowser
http://www.codeproject.com/Articles/6554/How-to-use-HttpWebRequest-and-HttpWebResponse-in-N
http://ycouriel.blogspot.com/2010/07/webbrowser-and-httpwebrequest-cookies.html
http://stackoverflow.com/questions/15049877/c-sharp-getting-webbrowser-cookies-to-log-in


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_THIRD_PARTY = 0x10;
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;
}