WebBrowser的Cookie操作

.在WebBrowser中获取Cookie

CookieContainer myCookieContainer = new CookieContainer();

string cookieStr = webBrowser1.Document.Cookie;
string[] cookstr = cookieStr.Split(';');
foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
Cookie ck = new Cookie(cookieNameValue[].Trim ().ToString(), cookieNameValue[].Trim ().ToString());
ck.Domain = "www.google.com";
myCookieContainer.Add(ck);
} WebClient设置cookie!
WebClient wc = new WebClient();
wc.Headers.Add("Cookie", "PHPSESSID=" + cookie + ";");
// 注意,这里是Cookie,不是Set-Cookie
byte[] re = wc.UploadData(Global.RootPath + "test.php", new byte[]);
System.Text.UTF8Encoding converter = new System.Text.UTF8Encoding();
string str = converter.GetString(re); . 在WebBrowser中设置Cookie
public partial class WebBrowserControl : Form
{
private String url; [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData); public WebBrowserControl(String path)
{
this.url = path;
InitializeComponent(); // set cookie
InternetSetCookie(url, "JSESSIONID", Globals.ThisDocument.sessionID); // navigate
webBrowser.Navigate(url);
}
} .将WebBrowser的cookie信息传给HttpWebRequest 先建一个"CookieContainer" 把WebBrowser中的Cookie保存在里面 //在WebBrowser中登录cookie保存在WebBrowser.Document.Cookie中
CookieContainer myCookieContainer = new CookieContainer(); //String 的Cookie 要转成 Cookie型的 并放入CookieContainer中
string cookieStr = webBrowser1.Document.Cookie;
string[] cookstr = cookieStr.Split(';'); foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
Cookie ck = new Cookie(cookieNameValue[].Trim().ToString(), cookieNameValue[].Trim().ToString());
ck.Domain = "www.abc.com";//必须写对
myCookieContainer.Add(ck);
} HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create("http://www.abc.com/search.asp");
hreq.Method = "POST";
hreq.ContentType = "application/x-www-form-urlencoded"; //自己创建的CookieContainer
hreq.CookieContainer = myCookieContainer; string postdata = "id=2005&action=search&name=";
byte[] byte1 = Encoding.ASCII.GetBytes(postdata);
hreq.ContentLength = byte1.Length; Stream poststream = hreq.GetRequestStream();
poststream.Write(byte1, , byte1.Length);
poststream.Close(); HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();

WebBrowser Cookie的更多相关文章

  1. 【总结】清除webbrowser cookie/session的6种方法

    下面是我测试下来的6种清除webbrowser中cookie的6种方法: //方法一:调用 wininet.dll清除cookie (推荐) SuppressWininetBehavior(); // ...

  2. 【C#】清除webBrowser 缓存和Cookie的解决方案

    试了很多方法,最后发现万剑大哥的方法管用,转载一下 转自:https://www.cnblogs.com/midcn/p/3527123.html 通过测试webBrowser与IE缓存和Cookie ...

  3. WebBrowser的Cookie操作之流量刷新机

    最近一直在思考着如何通过代码去伪装或实现人工自然浏览网页的效果,起初能想到的是用WebBrowser实现这一效果,需要达到的功能预想有以下几点: 1.自动刷新 2.模拟人工下拉滚动条并停留一段时间: ...

  4. C# WebBrowser HttpWebRequest Cookie 的结合运用。

    在WebBrowser下对网页进行操作其实是一件挺轻松的事情,他可以很方便实现自定义的网站访问习惯.而WebBrowser毕竟是对MS原生 控件的封装,当我们使用C#下的WebBrowser尤其是这样 ...

  5. WebBrowser设置Cookie

    在winform里面经常会用到WebBrowser,这是一个难点就是如何设置cookies,注意,Docment对象是只读的,所以WebBrowser.Docment.cookie也就只有get方法, ...

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

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

  7. c# 获取 webbrowser 完整 cookie

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

  8. 【wpf WebBrowser 清空网站的Cookie&Session 清空用户登录状态】

    最近做项目遇到了一个说小不小,说大不大的问题,那就是在WebBrowser中清空网站上用户的登陆状态, 一开始心想,那不就清空cookies就行啦,那么简单的事情,百度一下 …… …… 是的,正如你们 ...

  9. 网上收集的WebBrowser的Cookie操作

    原文:网上收集的WebBrowser的Cookie操作 1.WebBrowser设置Cookie Code highlighting produced by Actipro CodeHighlight ...

随机推荐

  1. spark streaming中维护kafka偏移量到外部介质

    spark streaming中维护kafka偏移量到外部介质 以kafka偏移量维护到redis为例. redis存储格式 使用的数据结构为string,其中key为topic:partition, ...

  2. Round #2

    题源:来自hzwer整理的题 2014-5-10 NOIP模拟赛 by coolyangzc Problem 1 机器人(robot.cpp/c/pas) [题目描述] 早苗入手了最新的Gundam模 ...

  3. Android Gradle Task-中文

    任务可以从根项目运行 Android 任务 androidDependencies-显示项目的Android依赖项 signingReport-显示基础和测试模块的签名信息 sourceSets-打印 ...

  4. Spark 异步Action

    异步不保序,但大作业执行时间后移. .set("spark.scheduler.mode", "FAIR") 公平调度,充分使用集群资源. Spark Doc ...

  5. JS Fetch

    使用Fetch 1.进行 fetch 请求 一个基本的 fetch请求设置起来很简单.看看下面的代码: fetch('http://example.com/movies.json') .then(fu ...

  6. VSCode调试Flutter的问题解决

    错误:Target of URI doesn't exist: package:fultter/material.dart 原因:flutter版本不匹配.flutter升级到了最新版本,而项目引用的 ...

  7. 惊奇!用Java也能实现比特币系统

    最近区块链技术突然爆火,身边做技术的朋友茶余饭后不谈点区块链什么的都被认为是跟不上时代了,为啥会这样了? 这其实跟比特币价格去年的突飞猛进是分不开的,比特币价格从去年初不到一千美金到今年初最高接近两万 ...

  8. [Swift]LeetCode84. 柱状图中最大的矩形 | Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. [Swift]LeetCode122. 买卖股票的最佳时机 II | Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  10. [Swift]LeetCode228. 汇总区间 | Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...