我正在使用 WebBrowser 登录网站,然后我想使用正则表达式获取一些数据,但是 webRequest 没有使用 web Browse cookie,
我的浏览器是公开的, 有没有办法在 webRequest 中使用 WebBrowser cookie?
最佳答案
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;
}
这适用于大多数网站,但使用子域的网站可能会出现问题。
关于c# WebRequest 使用 WebBrowser cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/650536/