[转载]WebBrowser的Cookie操作(与CookieContainer的关系)(转载) - 蓝雪原 - 博客园

[转载]WebBrowser的Cookie操作(与CookieContainer的关系)(转载) – 蓝雪原 – 博客园.

//在WebBrowser中登录cookie保存在WebBrowser.Document.Cookie中
readonly CookieContainer myCookieContainer = new CookieContainer();

private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://t.qq.com");
}

private void button2_Click(object sender, EventArgs e)
{
//String 的Cookie 要转成 Cookie型的 并放入CookieContainer中
if (webBrowser1.Document != null)
{
string cookieStr = webBrowser1.Document.Cookie;
string[] cookstr = cookieStr.Split(';');

foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');

string strValue = cookieNameValue[1].Trim().Replace(",", "%2C");

Cookie ck = new Cookie(cookieNameValue[0].Trim(), strValue);
ck.Domain = "t.qq.com"; //必须写对
myCookieContainer.Add(ck);
}
}

var pageDownLoadNew = new PageDownLoad { Cookies = myCookieContainer };
pageDownLoadNew.GetHtmlCode("http://t.qq.com");
webBrowser2.DocumentText = pageDownLoadNew.HtmlCode;
}

引用地址:http://blog.csdn.net/ruixue0117/article/details/8265698

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏