using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text; internal sealed class NativeMethods
{
#region enums public enum ErrorFlags
{
ERROR_INSUFFICIENT_BUFFER = ,
ERROR_INVALID_PARAMETER = ,
ERROR_NO_MORE_ITEMS =
} public enum InternetFlags
{
INTERNET_COOKIE_HTTPONLY = , //Requires IE 8 or higher
INTERNET_COOKIE_THIRD_PARTY = ,
INTERNET_FLAG_RESTRICTED_ZONE =
} #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
} /// <SUMMARY></SUMMARY>
/// 取得WebBrowser的完整Cookie。
/// 因为默认的webBrowser1.Document.Cookie取不到HttpOnly的Cookie
///
public class FullWebBrowserCookie
{ [SecurityCritical]
public static string GetCookieInternal(Uri uri, bool throwIfNoCookie)
{
uint pchCookieData = ;
string url = UriToString(uri);
uint flag = (uint)NativeMethods.InternetFlags.INTERNET_COOKIE_HTTPONLY; //Gets the size of the string builder
if (NativeMethods.InternetGetCookieEx(url, null, null, ref pchCookieData, flag, IntPtr.Zero))
{
pchCookieData++;
StringBuilder cookieData = new StringBuilder((int)pchCookieData); //Read the cookie
if (NativeMethods.InternetGetCookieEx(url, null, cookieData, ref pchCookieData, flag, IntPtr.Zero))
{
DemandWebPermission(uri);
return cookieData.ToString();
}
} int lastErrorCode = Marshal.GetLastWin32Error(); if (throwIfNoCookie || (lastErrorCode != (int)NativeMethods.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), ).ToString();
}
}

FullWebBrowserCookie 取得WebBrowser的完整Cookie的更多相关文章

  1. C#利用WebBrowser获取完整COOKIE

    代码: http://www.cnblogs.com/hsapphire/archive/2010/09/10/1823384.html http://blog.csdn.net/attilax/ar ...

  2. WebBrowser获取完整COOKIE

    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern bool ...

  3. C#用WebBrowser与WIN API辅助模拟获取网站完整Cookie

    网上找到的可以完整获取Cookie的方法,转载一下希望能帮助更多人. 亲测可用 在Winform中使用WebBrowser控件获取网站的Cookie有时候是不完整的,默认调用Document.Cook ...

  4. C#获取webbrowser完整cookie

    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] //API设定Cookie stat ...

  5. 清除webBrowser 缓存和Cookie的解决方案

    通过测试webBrowser与IE缓存和Cookie都存放在Local Settings\Temporary Internet Files,我们可以直接调用IE API进行清除 解决方案1: publ ...

  6. 从WebBrowser中取得Cookie 和 WebClient设置cookie!

    原文:从WebBrowser中取得Cookie 和 WebClient设置cookie! 从WebBrowser中取得Cookie 的代码 CookieContainer myCookieContai ...

  7. c#后的完整cookie

    http://www.cnblogs.com/top5/archive/2010/04/11/1709457.html c#设置 webbrowser的请求cookie,可以通过fiddler分析co ...

  8. c# 获取 webbrowser 完整 cookie

    下面的代码实现的功能确实如标题所言,但要求是获取的是当前进程内的webbrowser,跨进程或引用的ShellWindows对象无效, 哎我本来两种情况都要用,只把前者代码先记下: internal ...

  9. webBrowser获取取Cookie不全的解决方法

    //取当前webBrowser登录后的Cookie值           [DllImport("wininet.dll", CharSet = CharSet.Auto, Set ...

随机推荐

  1. 9、redis之事务2-Jedis的八种调用方式(事务、管道、分布式)介绍

    1.普通同步 @Test public void test1Normal() { Jedis jedis = new Jedis("localhost"); long start ...

  2. 3、redis之java client环境搭建

    JAVA Client环境搭建 POM: <dependency> <groupId>redis.clients</groupId> <artifactId& ...

  3. CentOS7 通过代理上网

    1.修改/etc/profile,增加以下内容: http_proxy=http://[代理地址]:[代理地址的端口]/ https_proxy=http://[代理地址]:[代理地址的端口]/ ex ...

  4. Jmeter-maven-plugin高级配置之选择测试脚本(转)

    Posted on 2014 年 6 月 6 日 在pom.xml文件中可以指定运行哪些jmx脚本. 运行所有的测试脚本 Jmeter默认运行${project.base.directory}/src ...

  5. 用C读取json文件

    a jconf_t * read_jconf(const char *file) { static jconf_t conf; // 清空数据 memset(&conf, , sizeof(j ...

  6. 利用 WireShark 深入调试网络请求

    来源:伯乐在线 - bestswifter 如有好文章投稿,请点击 → 这里了解详情 背景 最近发现我们产品在打开广告链接(Webview)时有一定概率会非常慢,白屏时间超过 10s,追查广告的过程中 ...

  7. 【虚拟化系列】VMware vSphere 5.1 简介与安装

    一. VMware vSphere 5.1简介           vSphere是VMware推出的基于云计算的新一代数据中心虚拟化套件,提供了虚拟化基础架构.高可用性.集中管理.监控等一整套解决方 ...

  8. 百度地图 ijintui以及七牛、百度编辑器、kindeditor

    密码是明文存储的 sig错误是因为params没拼接上md5后的秘钥,测试时候可以在 Api\Controller\CommonController\_initialize 方法里注释掉效验的代码 代 ...

  9. SQL Server 表分区之水平表分区

    什么是表分区? 表分区分为水平表分区和垂直表分区,水平表分区就是将一个具有大量数据的表,进行拆分为具有相同表结构的若干个表:而垂直表分区就是把一个拥有多个字段的表,根据需要进行拆分列,然后根据某一个字 ...

  10. Android数据库安全解决方案,使用SQLCipher

    源码:http://files.cnblogs.com/android100/SQLCipherTest.rar 我们都知道,Android系统内置了SQLite数据库,并且提供了一整套的API用于对 ...