10

我正在使用 WebBrowser 登录网站,然后我想使用正则表达式来获取一些数据,但 webRequest 没有使用网络浏览 cookie,

我的 webBrowser 是公开的,有什么方法可以在 webRequest 中使用 WebBrowser cookie 吗?

4

3 回答 3

12
    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;
    }

这适用于大多数网站,但是使用子域的网站可能会出现问题。

于 2010-05-11T18:34:52.193 回答
7

您可以将 CookieContainer 用于 Web 请求。

 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;

...

于 2009-03-16T14:15:22.513 回答
0

这是银光吗?如果是这样,从 silverlight 3 开始,如果您使用浏览器网络堆栈,那么您应该免费获得 cookie。默认情况下,当您使用 WebRequest.Create() 方法创建 n HttpWebrequest 时,您会获得浏览器堆栈。请注意,如果您使用 CreateHTTP 方法,您将获得一个客户端堆栈,默认情况下它不包括浏览器 cookie(如前所述,您必须通过诡计来获取它们)

请参阅http://msdn.microsoft.com/en-us/library/dd920295(VS.95).aspx关于 silverlight 自版本 3 以来的网络堆栈

于 2010-10-20T22:05:46.200 回答