c# 获取 webbrowser 完整 cookie
下面的代码实现的功能确实如标题所言,但要求是获取的是当前进程内的webbrowser,跨进程或引用的ShellWindows对象无效, 哎我本来两种情况都要用,只把前者代码先记下: 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
} Here is an example for the usage: [SecurityCritical]
private 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();
}
c# 获取 webbrowser 完整 cookie的更多相关文章
- C#获取webbrowser完整cookie
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] //API设定Cookie stat ...
- 获取WebBrowser全cookie 和 httpWebRequest 异步获取页面数据
获取WebBrowser全cookie [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true) ...
- C#用WebBrowser与WIN API辅助模拟获取网站完整Cookie
网上找到的可以完整获取Cookie的方法,转载一下希望能帮助更多人. 亲测可用 在Winform中使用WebBrowser控件获取网站的Cookie有时候是不完整的,默认调用Document.Cook ...
- webBrowser.Document.Cookie取不到HttpOnly的Cookie,取Cookie不完整
在做数据采集时,有些网站需要输入验证码,但各网站验证码都不同,不可能有完美的识别验证码的代码,所以我也没去研究,我所采取的方案是:在winform里通过WebBrowser调用网页先手动登录系统,然后 ...
- FullWebBrowserCookie 取得WebBrowser的完整Cookie
using System; using System.ComponentModel; using System.Net; using System.Runtime.InteropServices; u ...
- C# WebBrowser HttpWebRequest Cookie 的结合运用。
在WebBrowser下对网页进行操作其实是一件挺轻松的事情,他可以很方便实现自定义的网站访问习惯.而WebBrowser毕竟是对MS原生 控件的封装,当我们使用C#下的WebBrowser尤其是这样 ...
- WebBrowser的Cookie操作之流量刷新机
最近一直在思考着如何通过代码去伪装或实现人工自然浏览网页的效果,起初能想到的是用WebBrowser实现这一效果,需要达到的功能预想有以下几点: 1.自动刷新 2.模拟人工下拉滚动条并停留一段时间: ...
- c++ builder TIdHttp 获取不到cookie
用c++ builder 的TIdHttp组件Get一个ASP.Net MVC服务器的一个页面,获取页面中Cookie信息,修改后Post到服务器上去. 在本地调试的时候可以获取到,部署到服务器上就获 ...
- WebBrowser设置Cookie
在winform里面经常会用到WebBrowser,这是一个难点就是如何设置cookies,注意,Docment对象是只读的,所以WebBrowser.Docment.cookie也就只有get方法, ...
随机推荐
- paramiko堡垒机、线程及锁
1.使用paramiko实现ssh连接和scp拷贝 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 1.1 SSHClient 用于连接远 ...
- 链表(C++语言实现)
我使用的是严蔚敏的数据结构C语言版,很反感里面的全是伪代码,平常也没怎么敲代码,链表和顺序表是数据结构的第一个实验课程,顺序表还好,但是链表就有点困难了,因为里面涉及指针的运用.我对于指针并不是很精通 ...
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState解决?
做项目到最后整合的时候测试的时候发现 切换tab更换fragment的时候抛出了这个异常,根据异常信息Can not perform this action after onSaveInstance ...
- 【LEETCODE OJ】Copy List with Random Pointer
Problem link: http://oj.leetcode.com/problems/copy-list-with-random-pointer/ Deepcopy a linked list ...
- Java 集合深入理解(13):Stack 栈
点击查看 Java 集合框架深入理解 系列, - ( ゜- ゜)つロ 乾杯~ 今天心情不错,再来一篇 Stack ! 数据结构中的 栈 数据结构中,栈是一种线性数据结构,遵从 LIFO(后进先出)的操 ...
- 【转】C 宏
http://www.cs.yale.edu/homes/aspnes/pinewiki/C%282f%29Macros.html See KernighanRitchie Appendix A12. ...
- Runtime 、 Block
1 使用Block方式,对学生对象进行排序. 1.1 问题 在iOS4.0+ 和Mac OS X 10.6+ 中添加了Block概念,以对C语言进行扩展.在Block中可以定义参数列表.返回类型,还可 ...
- Cannot retrieve definition for form bean null on action错误
Cannot retrieve definition for form bean null on action错误 1. 如果jsp页面中要用到<html:form action="& ...
- Centos 6.4 8250/16550 只生成了4个串口
/********************************************************************* * Centos 6.4 8250/16550 只生成了4 ...
- 《JS高程》JS-Object对象整理
继上篇<JS高程>对象&原型笔记,对JavaScript中的Object对象进行了整理,梳理一遍~ 参考文章:详解Javascript中的Object对象 ------------ ...