.net 獲取瀏覽器Cookie(包括HttpOnly)實(shí)例分享
更新時(shí)間:2013年10月09日 14:35:13 作者:
這篇文章介紹了.net 獲取瀏覽器Cookie(包括HttpOnly)實(shí)例,有需要的朋友可以參考一下
一、接口文件
復(fù)制代碼 代碼如下:
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類
復(fù)制代碼 代碼如下:
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
{
/// <SUMMARY></SUMMARY>
/// 取得WebBrowser的完整Cookie。
/// 因?yàn)槟J(rèn)的webBrowser1.Document.Cookie取不到HttpOnly的Cookie
/// IE7不兼容,IE8可以,其它未知
///
public class FullWebBrowserCookie
{
public static Dictionary<string, string> GetCookieList(Uri uri, bool throwIfNoCookie)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
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() + "項(xiàng)");
return dict;
}
public static string GetCookieValue(string key, Uri uri, bool throwIfNoCookie)
{
Console.WriteLine("GetCookieValue");
Dictionary<string, string> dict = GetCookieList(uri, throwIfNoCookie);
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();
}
}
}
相關(guān)文章
關(guān)于ASP.NET頁面打印技術(shù)的常用方法總結(jié)
B/S結(jié)構(gòu)導(dǎo)致了Web應(yīng)用程序中打印的特殊性;程序運(yùn)行在瀏覽器中,打印機(jī)在本地,而文件確可能在服務(wù)器上,導(dǎo)致了打印控制不是很靈活,接下來介紹幾種常見的打印技術(shù),感興趣的朋友可以了解下2013-01-01
asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼
asp.net(C#) 生成隨機(jī)驗(yàn)證碼的代碼...2007-04-04
ASP.NET(C#) Web Api通過文件流下載文件的實(shí)例
這篇文章主要介紹了ASP.NET(C#) Web Api通過文件流下載文件的方法,提供源碼下載,需要的朋友可以參考下。2016-06-06
動(dòng)態(tài)代理的5模式使用示例和Mixin模式
什么叫"動(dòng)態(tài)代理",代理模式我們都知道,動(dòng)態(tài)代理就是動(dòng)態(tài)生成的代理(采用Emit)。5種代理模式:ClassProxy、ClassProxyWithTarget、InterfaceProxyWithoutTarget、InterfaceProxyWithTarget、InterfaceProxyWithTargetInterface、Mixin模式2013-11-11
基于ASP.NET+easyUI框架實(shí)現(xiàn)圖片上傳功能(判斷格式+即時(shí)瀏覽 )
這篇文章主要介紹了基于ASP.NET+easyUI框架實(shí)現(xiàn)圖片上傳功能的相關(guān)資料,重點(diǎn)在于如何判斷格式,實(shí)現(xiàn)即時(shí)瀏覽,需要的朋友可以參考下2016-06-06
在.NET使用JSON作為數(shù)據(jù)交換格式實(shí)例演示
JSON(JavaScript Object Notation)是一種輕量級(jí)輕量級(jí)的數(shù)據(jù)交換格式,并且它獨(dú)立于編程語言,接下來為大家介紹下使用JSON作為數(shù)據(jù)交換格式在.net中的應(yīng)用2013-03-03

