博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net 获取浏览器Cookie(包括HttpOnly)
阅读量:5145 次
发布时间:2019-06-13

本文共 4923 字,大约阅读时间需要 16 分钟。

网上好不容易找到的,分享+收藏

一、接口文件

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
GetCookieList(Uri uri, bool throwIfNoCookie) { Dictionary
dict = new 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
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(); } }}

  

 

 

转载于:https://www.cnblogs.com/suger/p/3359146.html

你可能感兴趣的文章
LeetCode|Add Two Numbers
查看>>
如果一个女生说,她集齐了十二个星座的前男友,我们应该如何估计她前男友的数量?...
查看>>
ubuntu配置XManager可用
查看>>
Version Control&Git
查看>>
两难的情况该如何处理
查看>>
Git -- 版本回退
查看>>
sqlite多表关联update
查看>>
xss---攻击
查看>>
java基础(六)--- list
查看>>
注解式struts2配合ajax
查看>>
自己写的一个 java swing 的闹钟
查看>>
OpenCV 中Scalar
查看>>
PHP 用Class构造JSON数据
查看>>
vue中js里的/* eslint-disable*/
查看>>
Git 代码更新:git fetch 和 git pull 的区别
查看>>
第4次作业-案例分析
查看>>
[转载]AMOLED结构详解,BOE专家给你分析驱动补偿
查看>>
动态调用web服务
查看>>
English trip -- VC(情景课)2 A At school
查看>>
English trip M1 - PC1 Are you a Model? 你是模特吗? Teacher:Taylor
查看>>