c# WebRequest 使用 WebBrowser cookie [英] c# WebRequest using WebBrowser cookie
本文介绍了c# WebRequest 使用 WebBrowser cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 WebBrowser 登录一个站点,然后我想使用正则表达式来获取一些数据,但是 webRequest 没有使用 Web 浏览 cookie,
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 ,
我的网页浏览器是公开的,有没有办法在 webRequest 中使用 WebBrowser cookie?
my webBrowser is in public , is there any way to using WebBrowser cookie in webRequest ?
推荐答案
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;
}
这适用于大多数网站,但是使用子域的网站可能会出现问题.
This will work on most sites, however sites that use subdomains might be a problem.
这篇关于c# WebRequest 使用 WebBrowser cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文