原文:从WebBrowser中取得Cookie 和 WebClient设置cookie!
从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.5ilrc.com";
myCookieContainer.Add(ck);
}
WebClient设置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);