日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/18 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WebBrowser的Cookie操作(与CookieContainer的关系) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

WebBrowser的Cookie操作

1.在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[0].Trim ().ToString(), cookieNameValue[1].Trim ().ToString());ck.Domain = "www.google.com";myCookieContainer.Add(ck); }

WebClient設(shè)置cookie!

WebClient wc = new WebClient(); wc.Headers.Add("Cookie", "PHPSESSID=" + cookie + ";"); // 注意,這里是Cookie,不是Set-Cookie byte[] re = wc.UploadData(Global.RootPath + "test.php", new byte[0]); System.Text.UTF8Encoding converter = new System.Text.UTF8Encoding(); string str = converter.GetString(re);

2. 在WebBrowser中設(shè)置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 cookieInternetSetCookie(url, "JSESSIONID", Globals.ThisDocument.sessionID);// navigatewebBrowser.Navigate(url);} }

3.將WebBrowser的cookie信息傳給HttpWebRequest

先建一個(gè)"CookieContainer" 把WebBrowser中的Cookie保存在里面

//在WebBrowser中登錄cookie保存在WebBrowser.Document.Cookie中 CookieContainer myCookieContainer = new CookieContainer();//String 的Cookie 要轉(zhuǎn)成 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[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());ck.Domain = "www.abc.com";//必須寫對(duì)myCookieContainer.Add(ck); }HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create("http://www.abc.com/search.asp"); hreq.Method = "POST"; hreq.ContentType = "application/x-www-form-urlencoded";//自己創(chuàng)建的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, 0, byte1.Length); poststream.Close();HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();

轉(zhuǎn)載于:https://www.cnblogs.com/xiongrx/archive/2012/12/06/5514501.html

總結(jié)

以上是生活随笔為你收集整理的WebBrowser的Cookie操作(与CookieContainer的关系)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。