1: [DllImport("wininet.dll", SetLastError = true)]
2: public static extern bool InternetGetCookieEx(
3: string url,
4: string cookieName,
5: StringBuilder cookieData,
6: ref int size,
7: Int32 dwFlags,
8: IntPtr lpReserved);
9:
10: private const Int32 InternetCookieHttponly = 0x2000;
1: /// <summary>
2: /// Gets the URI cookie container.
3: /// </summary>
4: /// <param name="uri">The URI.</param>
5: /// <returns></returns>
6: public static CookieContainer GetUriCookieContainer(Uri uri)
7: {
8: CookieContainer cookies = null;
9: // Determine the size of the cookie
10: int datasize = 8192 * 16;
11: StringBuilder cookieData = new StringBuilder(datasize);
12: if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
13: {
14: if (datasize < 0)
15: return null;
16: // Allocate stringbuilder large enough to hold the cookie
17: cookieData = new StringBuilder(datasize);
18: if (!InternetGetCookieEx(
19: uri.ToString(),
20: null, cookieData,
21: ref datasize,
22: InternetCookieHttponly,
23: IntPtr.Zero))
24: return null;
25: }
26: if (cookieData.Length > 0)
27: {
28: cookies = new CookieContainer();
29: cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
30: }
31: return cookies;
32: }
1: private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
2:
3: [DllImport("wininet.dll", SetLastError = true)]
4: private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
5:
6: public static void ClearCookie()
7: {
8: InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0);
9: }