在WebBrowser中使用WebRequest cookie

时间:2012-02-25 11:11:46

标签: c# httpwebrequest webbrowser-control

我想使用WebRequest登录网站,稍后在WebBrowser中显示网站(已记录)。

但是如何将WebRequest Cookie“复制”到WebBrowser

提前致谢,

卡茨珀

1 个答案:

答案 0 :(得分:3)

使用cookie集合来抓取cookie,我本月写了类似的东西,我可以分享一些示例代码:

    static string GetFromServer(string URL, ref CookieCollection oCookie)
    {
        //first rquest
        // Create a request for the URL. 
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
        request.AllowAutoRedirect = false;
        // If required by the server, set the credentials.
        //request.Credentials = CredentialCache.DefaultCredentials;
        request.CookieContainer = new CookieContainer();
        if (oCookie != null)
        {
            foreach (Cookie cook in oCookie)
            {
                request.CookieContainer.Add(cook);
            }
        }

        // Get the response.
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        foreach (Cookie cook in response.Cookies)
        {
            oCookie.Add(cook);
        }
        // Display the status.
        while (response.StatusCode == HttpStatusCode.Found)
        {
            response.Close();
            request = (HttpWebRequest)HttpWebRequest.Create(response.Headers["Location"]);
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
            request.AllowAutoRedirect = false;
            request.CookieContainer = new CookieContainer();
            if (oCookie != null)
            {
                foreach (Cookie cook in oCookie)
                {
                    request.CookieContainer.Add(cook);
                }
            }
            response = (HttpWebResponse)request.GetResponse();
            foreach (Cookie cook in response.Cookies)
            {
                oCookie.Add(cook);
            }
        }
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams and the response.
        reader.Close();
        response.Close();
        return responseFromServer;
    }

现在你有了cookie,你只需要将它设置为webBrowser控件,导入这个方法:

    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);

并在获得Cookie后调用此方法:

    string cookie_string = string.Empty;
        foreach (Cookie cook in cookieCon)
        {
            cookie_string += cook.ToString() + ";";
            InternetSetCookie(url, cook.Name, cook.Value);
        }
        webBrowser1.Navigate(url, "", null, "Cookie: " + cookie_string + Environment.NewLine);

请注意,这只是我的测试代码,主要是从msdn复制的,所以它可能有错误,你可能需要更多的异常处理