我使用WebBrowser登录网站,然后我想使用正则表达式获取一些数据,但webRequest没有使用web Browse cookie,
我的webBrowser是公开的,有没有办法在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; }
这适用于大多数网站,但使用子域的网站可能存在问题.
您可以将CookieContainer用于Webrequest.
web_cookies = new CookieContainer(); // Create a 'WebRequest' object with the specified url. HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url); myWebRequest.CookieContainer = web_cookies;
希望这可以帮助.
好的,你想登录.这就是不同的故事.您可以使用NetworkCredential.
public string get_secure_webpage(string url, string username, string password) { WebRequest myWebRequest = WebRequest.Create(url); NetworkCredential networkCredential = new NetworkCredential(username, password); myWebRequest.Credentials = networkCredential;
...