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

一、接口文件

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
{
/// <SUMMARY></SUMMARY>
/// 取得WebBrowser的完整Cookie。
/// 因为默认的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() + "项"); 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();
}
}
}

  

.net 获取浏览器Cookie(包括HttpOnly)的更多相关文章

  1. JS读写浏览器cookie及读取页面参数

    JS读写浏览器cookie及读取页面参数 var zbrowser = { //设置浏览器cookie,exdays是cookie有效时间 setCookie: function (c_name, v ...

  2. Java+selenium如何清理浏览器Cookie

    一.场景:在未注销系统切换不同账号登录系统下,登录会有浏览器缓存,导致登录时间异常缓慢.跟开发浏览器缓存处理机制有关系. 二.解决方法: 获取浏览器Cookie,在Login方法前增加清除缓存的代码. ...

  3. 浏览器获取京东cookie

    电脑浏览器打开京东网址 https://m.jd.com/ 按键盘F12键打开开发者工具,然后点下图中的图标 此时是未登录状态(使用手机短信验证码登录),如已登录请忽略此步骤 使用手机短信验证码登录( ...

  4. Chrome浏览器Cookie解密

    做过 web 开发的都知道:浏览器会把重要的认证登录认证信息存放到 cookie 中,在 cookie 有效期内,再次访问这个网站的时候就可以直接从 cookie 中获取到登录信息,这样就可以实现自动 ...

  5. JavaScript获取浏览器信息的方法

    Window有navigator对象让我们得知浏览器的全部信息.我们可以利用一系列的API函数得知浏览器的信息. JavaScript代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 1 ...

  6. Session Cookie的HttpOnly和secure属性

    Session Cookie的HttpOnly和secure属性 一.属性说明: 1 secure属性 当设置为true时,表示创建的 Cookie 会被以安全的形式向服务器传输,也就是只能在 HTT ...

  7. JavaScript获取浏览器版本等信息

    ** 不同浏览器版本可能存在差异,使用时请测试自己的环境 ** 测试时各个浏览器版本 IE: 11.953.14393.0 Edge: Microsoft Edge 38.14393.0.0;Micr ...

  8. php设置cookie为httponly防止xss攻击

    什么是XSS攻击? XSS攻击(Cross Site Scripting)中文名为跨站脚本攻击,XSS攻击时web中一种常见的漏洞.通过XSS漏洞可以伪造目标用户登录,从而获取登录后的账号操作. 网站 ...

  9. Cookie的HttpOnly、secure、domain属性

    Cookie主要属性 Cookie主要属性: path domain max-age expires:是expires的补充,现阶段有兼容性问题:IE低版本不支持,所以一般不单独使用 secure h ...

随机推荐

  1. Sping-Spring JDBC框架

    JDBC框架概述 在使用普通的JDBC数据库时,就会很麻烦的写不必要的代码来处理异常,打开和关闭数据库连接等.但Spring JDBC框架负责所有的底层细节,从开始打开连接,准备和执行SQL语句,处理 ...

  2. POJ2480:Longge's problem(欧拉函数的应用)

    题目链接:传送门 题目需求: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N ...

  3. 细说PHP7

    PHP7带来的新东西 1.类型的声明. 可以使用字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool),来声明函数的参数类型与函数返回值. declare(s ...

  4. python 中list的操作(循环、切片、增、删、改、查、反转、排序)

    列表的索引(下标)从0开始,最后一个可以用-1表示. 1. 循环 如果直接for 循环一个list 的时候,那么每次循环的都是这个List里的元素 2. 切片 可指定步长进行取值,步长默认为1 3. ...

  5. centos7编译安装Python3.6(与2.7并存)

    首先去官网下载python3.6 https://www.python.org/ 环境准备:#yum install openssl-devel bzip2-devel expat-devel gdb ...

  6. Flask之flask-script模块使用

    Flask Script扩展提供向Flask插入外部脚本的功能,包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,及其他运行在web应用之外的命令行任 ...

  7. XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem J. Terminal

    题目:Problem J. TerminalInput file: standard inputOutput file: standard inputTime limit: 2 secondsMemo ...

  8. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Changed

    地址:http://codeforces.com/contest/782/problem/B 题目: B. The Meeting Place Cannot Be Changed time limit ...

  9. googleMapReduce

    摘要 MapReduce是一个编程模型,也是一个处理和生成超大数据集的算法模型的相关实现.用户首先创建一个Map函数处理一个基于key/value pair的数据集合,输出中间的基于key/value ...

  10. cocos2dx 3.x 触摸事件

    单点触摸: bool onTouchBegan(cocos2d::Touch *pTouch, cocos2d::Event *pEvent); void onTouchMoved(cocos2d:: ...