您的位置:首页 > 理论基础 > 计算机网络

将WebBrowser的cookie信息传给HttpWebRequest

2010-12-28 22:33 267 查看
1、WebBrowser设置Cookie

1public partial class WebBrowserControl : Form
2 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
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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: