FullWebBrowserCookie 取得WebBrowser的完整Cookie
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的更多相关文章
- C#利用WebBrowser获取完整COOKIE
代码: http://www.cnblogs.com/hsapphire/archive/2010/09/10/1823384.html http://blog.csdn.net/attilax/ar ...
- WebBrowser获取完整COOKIE
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern bool ...
- C#用WebBrowser与WIN API辅助模拟获取网站完整Cookie
网上找到的可以完整获取Cookie的方法,转载一下希望能帮助更多人. 亲测可用 在Winform中使用WebBrowser控件获取网站的Cookie有时候是不完整的,默认调用Document.Cook ...
- C#获取webbrowser完整cookie
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] //API设定Cookie stat ...
- 清除webBrowser 缓存和Cookie的解决方案
通过测试webBrowser与IE缓存和Cookie都存放在Local Settings\Temporary Internet Files,我们可以直接调用IE API进行清除 解决方案1: publ ...
- 从WebBrowser中取得Cookie 和 WebClient设置cookie!
原文:从WebBrowser中取得Cookie 和 WebClient设置cookie! 从WebBrowser中取得Cookie 的代码 CookieContainer myCookieContai ...
- c#后的完整cookie
http://www.cnblogs.com/top5/archive/2010/04/11/1709457.html c#设置 webbrowser的请求cookie,可以通过fiddler分析co ...
- c# 获取 webbrowser 完整 cookie
下面的代码实现的功能确实如标题所言,但要求是获取的是当前进程内的webbrowser,跨进程或引用的ShellWindows对象无效, 哎我本来两种情况都要用,只把前者代码先记下: internal ...
- webBrowser获取取Cookie不全的解决方法
//取当前webBrowser登录后的Cookie值 [DllImport("wininet.dll", CharSet = CharSet.Auto, Set ...
随机推荐
- 使用fuel6.0自己主动安装openstack-juno版本号(2)
上篇中fuel_master已经安装完毕.接下来我们介绍怎样新建一个openstack环境. 1,在web界面中新建openstack环境: 点击主界面的加入button,新建openstack环境. ...
- tcp 两个重要窗口:滑动窗口 和 拥塞窗口
一:滑动窗口是接受数据端使用的窗口大小,用来告知发送端接收端的缓存大小,以此可以控制发送端发送数据的大小,从而达到流量控制的目的,对应==>rwnd:接收端窗口(receiver window) ...
- Android中怎样做到自己定义的广播仅仅能有指定的app接收
今天没吊事.又去面试了,详细哪家公司就不说了,由于我在之前的blog中注明了那些家公司的名字,结果人家给我私信说我泄露他们的题目.好吧,我错了... 事实上当我们已经在工作的时候.我们能够在空暇的时间 ...
- spring boot 服务 正确关闭方式
引言 Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行.产品级别的 ...
- excel怎么在插入的方框上打勾
本例主要介绍如何在excel中插入带对勾的方框. 工具/原料 Excel 操作步骤: 在编辑Excel表格模板时,比如说简历.人力信息登记表等,经常需要有一些可选项,如下例的婚姻状况就包括“有配 ...
- 基于git 客户端使用shell工具
1 定义全局启动 命令别名 C:\Program Files\Git\etc\profile.d\aliases.sh alias ls='ls -F --color=auto --show-cont ...
- Html Agility Pack - API
Html Agility Pack - APIParserSelectorsManipulationTraversingWriterUtilitiesAttributes HTML Parser HT ...
- 生命周期方法调用,以及在onStop()方法中处理草稿信息
生命周期方法调用顺序 1. 从会话列表界面跳转到信息列表界面. 07-17 17:29:18.718: I/txrjsms(19370): MessageListActivity.onCreate 0 ...
- ios中Pldatabase的用法(3)
#import "ViewController.h" @interface ViewController () @property(nonatomic,retain)PLSqlit ...
- e藏在哪里?
e约等于2.718,是和pi齐名的自然常数. 在任何一个均匀随机器中,都藏着e. import random def count(): k = 0 s = 0 while s < 1: k += ...