WebBrowser的Cookie操作(与CookieContainer的关系)(转载)

 1  //在WebBrowser中登录cookie保存在WebBrowser.Document.Cookie中      
 2         readonly CookieContainer myCookieContainer = new CookieContainer();
 3 
 4         private void button1_Click(object sender, EventArgs e)
 5         {
 6             webBrowser1.Navigate("http://t.qq.com");
 7         }
 8 
 9         private void button2_Click(object sender, EventArgs e)
10         {
11             //String 的Cookie 要转成 Cookie型的 并放入CookieContainer中  
12             if (webBrowser1.Document != null)
13             {
14                 string cookieStr = webBrowser1.Document.Cookie;
15                 string[] cookstr = cookieStr.Split(';');
16 
17                 foreach (string str in cookstr)
18                 {
19                     string[] cookieNameValue = str.Split('=');
20 
21                     string strValue = cookieNameValue[1].Trim().Replace(",", "%2C");
22 
23                     Cookie ck = new Cookie(cookieNameValue[0].Trim(), strValue);
24                     ck.Domain = "t.qq.com"; //必须写对  
25                     myCookieContainer.Add(ck);
26                 }
27             }
28 
29             var pageDownLoadNew = new PageDownLoad { Cookies = myCookieContainer };
30             pageDownLoadNew.GetHtmlCode("http://t.qq.com");
31             webBrowser2.DocumentText = pageDownLoadNew.HtmlCode;
32         }

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

posted @ 2014-06-20 16:55  DR19  阅读(367)  评论(0编辑  收藏  举报