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 ...
随机推荐
- 〖Linux〗转换Socks Proxy为Http Proxy
使用工具,privoxy,官网: http://www.privoxy.org/ socks5 proxy设定方法: autossh -CgNfD 0.0.0.0:1080 vps-lxb sock ...
- django之创建第6个项目-过滤器
1.views.PY # Create your views here. #coding:utf-8 from django.http import HttpResponse import datet ...
- HttpServletResponse对象(二)
一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,
- 数据库行列转换sql
经常折腾数据库,常常遇到数据库行列转换的问题,下面就用一个小例子来演示下如何进行行列转换. 1.创建一张表 CREATE TABLE [android_source]( [CREATETIME] [d ...
- 监听Sms.Content_URI而不是Sms.Inbox.CONTENT_URI
getContentResolver().registerContentObserver(Sms.Inbox.CONTENT_URI, true, newMsgObserver); / ...
- Tensorflow中的name_scope和variable_scope
Tensorflow是一个编程模型,几乎成为了一种编程语言(里面有变量.有操作......). Tensorflow编程分为两个阶段:构图阶段+运行时. Tensorflow构图阶段其实就是在对图进行 ...
- django日志使用TimeRotateFileHandler
如果使用django的settings.py设置日志会产生一些问题. 问题描述 报错信息如下: Traceback (most recent call last): File "C:\Pyt ...
- Quartz中时间表达式的设置-----corn表达式 (转)
Quartz中时间表达式的设置-----corn表达式 (注:这是让我看比较明白的一个博文,但是抱歉,没有找到原作者,如有侵犯,请告知) 时间格式: <!-- s m h d m w(?) y( ...
- 关于 Dropout 防止过拟合的问题
关于 Dropout 可以防止过拟合,出处:深度学习领域大神 Hinton,在2012年文献:<Improving neural networks by preventing co-adapta ...
- 【RS】List-wise learning to rank with matrix factorization for collaborative filtering - 结合列表启发排序和矩阵分解的协同过滤
[论文标题]List-wise learning to rank with matrix factorization for collaborative filtering (RecSys '10 ...