一、接口文件
复制代码 代码如下:
using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
namespace cookieHandler
{
internal sealed class INativeMethods
{
#region enums
public enum ErrorFlags
{
ERROR_INSUFFICIENT_BUFFER = 122,
ERROR_INVALID_PARAMETER = 87,
ERROR_NO_MORE_ITEMS = 259
}
public enum InternetFlags
{
INTERNET_cookie_HTTPonLY = 8192, //Requires IE 8 or higher
INTERNET_cookie_THIRD_PARTY = 131072,
INTERNET_FLAG_RESTRICTED_ZONE = 16
}
#endregion
#region DLL imports
[SuppressUnmanagedCodeSecurity, SecurityCritical, Dllimport("wininet.dll", EntryPoint = "InternetGetcookieExW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
internal static extern bool InternetGetcookieEx([In] string Url, [In] string cookieName, [Out] StringBuilder cookieData, [In, Out] ref uint pchcookieData, uint flags, IntPtr reserved);
#endregion
}
}
二、获取cookie类
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
namespace cookieHandler
{
///
/// 取得WebBrowser的完整cookie。
/// 因为默认的webBrowser1.document.cookie取不到HttpOnly的cookie
/// IE7不兼容,IE8可以,其它未知
///
public class FullWebBrowsercookie
{
public static Dictionary
{
Dictionary
string cookie = GetcookieInternal(uri, throwIfNocookie);
Console.WriteLine("FullWebBrowsercookie - 所有cookie:" + cookie);
string[] arrcookie = cookie.Split(';');
foreach (var item in arrcookie)
{
string[] arr = item.Split('=');
string key = arr[0].Trim();
string val = "";
if (arr.Length >= 2)
{
val = arr[1].Trim();
}
if (!dict.ContainsKey(key))
{
dict.Add(key, val);
}
}
Console.WriteLine("FullWebBrowsercookie - cookie已载入dict,共" + dict.Count.ToString() + "项");
return dict;
}
public static string Getcookievalue(string key, Uri uri, bool throwIfNocookie)
{
Console.WriteLine("Getcookievalue");
Dictionary
if (dict.ContainsKey(key))
{
return dict[key];
}
return "";
}
[SecurityCritical]
public static string GetcookieInternal(Uri uri, bool throwIfNocookie)
{
Console.WriteLine("GetcookieInternal");
uint pchcookieData = 0;
string url = UriToString(uri);
uint flag = (uint)INativeMethods.InternetFlags.INTERNET_cookie_HTTPONLY;
//Gets the size of the string builder
if (INativeMethods.InternetGetcookieEx(url, null, null, ref pchcookieData, flag, IntPtr.Zero))
{
pchcookieData++;
StringBuilder cookieData = new StringBuilder((int)pchcookieData);
//Read the cookie
if (INativeMethods.InternetGetcookieEx(url, null, cookieData, ref pchcookieData, flag, IntPtr.Zero))
{
DemandWebPermission(uri);
return cookieData.ToString();
}
}
int lastErrorCode = Marshal.GetLastWin32Error();
if (throwIfNocookie || (lastErrorCode != (int)INativeMethods.ErrorFlags.ERROR_NO_MORE_ITEMS))
{
throw new Win32Exception(lastErrorCode);
}
return null;
}
private static void DemandWebPermission(Uri uri)
{
string uriString = UriToString(uri);
if (uri.IsFile)
{
string localPath = uri.LocalPath;
new FileIOPermission(FileIOPermissionAccess.Read, localPath).Demand();
}
else
{
new WebPermission(NetworkAccess.Connect, uriString).Demand();
}
}
private static string UriToString(Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
UriComponents components = (uri.IsAbsoluteUri ? UriComponents.AbsoluteUri : UriComponents.SerializationInfoString);
return new StringBuilder(uri.GetComponents(components, UriFormat.SafeUnescaped), 2083).ToString();
}
}
}



