网上收集的WebBrowser的Cookie操作

本文介绍如何使用WebBrowser控件设置Cookie,并演示了如何将WebBrowser中的Cookie信息传递给HttpWebRequest,以便进行后续的HTTP请求操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://xcily1.blog.163.com/blog/static/287169162008975834286/


1、WebBrowser设置Cookie

 1public partial class WebBrowserControl : Form
 2<img none';="" document.getelementbyid('codehighlighter1_50_596_open_text').style.display="none" ;="" document.getelementbyid('codehighlighter1_50_596_closed_image').style.display="inline" document.getelementbyid('codehighlighter1_50_596_closed_text').style.display="inline" ;"="" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top" style="border: 0px; max-width: 100%;">    {
 3        private String url;
 4
 5        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
 6        public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
 7
 8        public WebBrowserControl(String path)
 9<img none';="" document.getelementbyid('codehighlighter1_335_582_open_text').style.display="none" ;="" document.getelementbyid('codehighlighter1_335_582_closed_image').style.display="inline" document.getelementbyid('codehighlighter1_335_582_closed_text').style.display="inline" ;"="" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" style="border: 0px; max-width: 100%;">        {
10            this.url = path;
11            InitializeComponent();
12
13            // set cookie
14            InternetSetCookie(url, "JSESSIONID", Globals.ThisDocument.sessionID);
15
16            // navigate
17            webBrowser.Navigate(url);
18        }
19        
20}


 2、将WebBrowser的cookie信息传给HttpWebRequest.

先建一个"CookieContainer" 把WebBrowser中的Cookie保存在里面

//在WebBrowser中登录 cookie保存在 WebBrowser.Document.Cookie中      
 1          CookieContainer myCookieContainer = new CookieContainer();
 2
 3
 4            //String 的Cookie 要转成 Cookie型的 并放入CookieContainer中
 5            string cookieStr = webBrowser1.Document.Cookie;
 6            string[] cookstr = cookieStr.Split(';');
 7            foreach (string str in cookstr)
 8<img none';="" document.getelementbyid('codehighlighter1_302_584_open_text').style.display="none" ;="" document.getelementbyid('codehighlighter1_302_584_closed_image').style.display="inline" document.getelementbyid('codehighlighter1_302_584_closed_text').style.display="inline" ;"="" alt="" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top" style="border: 0px; max-width: 100%;">            {
 9                string[] cookieNameValue = str.Split('=');
10                Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
11                ck.Domain = "www.abc.com";//必须写对
12                myCookieContainer.Add(ck);
13            }
14
15            HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create("http://www.abc.com/search.asp");
16            hreq.Method = "POST";
17            hreq.ContentType = "application/x-www-form-urlencoded";
18         
19            //自己创建的CookieContainer
20            hreq.CookieContainer = myCookieContainer;
21         
22            string postdata = "id=2005&action=search&name=";
23            byte[] byte1 = Encoding.ASCII.GetBytes(postdata);
24            hreq.ContentLength = byte1.Length;
25          
26            Stream poststream = hreq.GetRequestStream();
27            poststream.Write(byte1, 0, byte1.Length);
28            poststream.Close();
29      
30            HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值