问题如下:
I am logging into a site using a WebBrowser, then i want use regex to get some data , but webRequest didnt use web Browse cookie ,
my webBrowser is in public , is there any way to useing webbrowser cookie in webrequest ?
(想要HttpWebResponse取得webbrowser的cookie。
比如webbrowser已经在某网站登录了,那使用HttpWebResponse取得该网站内容时也要显示是登录的状态。
)
解决方法:
public CookieContainer GetCookieContainer()
{
CookieContainer container = new CookieContainer();
foreach (string cookie in webBrowser1.Document.Cookie.Split(';'))
{
string name = cookie.Split('=')[0];
string value = cookie.Substring(name.Length + 1);
string path = "/";
string domain = ".google.com"; //change to your domain name
container.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
}
return container;
}
如下方法不可用:(本人以试多次,不知何原因,愿高深的您能赐教)
//String 的Cookie 要转成 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";//必须写对
myCookieContainer.Add(ck);
}