使用C#中的浏览器控件处理Cookie

时间:2012-03-01 19:48:11

标签: c# automation httpwebrequest webbrowser-control

我正在网站上自动化流程。为简单起见,我们假设该过程分为两部分。第一个是登录网站,第二个是点击页面上的按钮。我相信登录机制使用cookie来处理身份验证。我使用过Fiddler并且能够查看此cookie。

我遇到的问题是,到目前为止,我可以自动登录并单击按钮,但我只能为一个控件执行此操作。我只有一次登录,系统不允许我使用其他浏览器再次登录。我想要做的是发出多个请求同时点击按钮。但是现在我被迫按顺序做了。

有没有办法可以从浏览器控件中获取Cookie并将其用于其他网络请求?

1 个答案:

答案 0 :(得分:0)

您可以使用HttpRequest和响应对象发出自己的请求。

以下功能可以帮助您解决此问题。通过使用它,您不需要一次又一次地登录,您只需将cookie添加到请求中,该请求将提供身份验证,您只需循环发送请求:

public static bool SessionRequest(Fiddler.Session oS, ref string htmlContent, string requestMethod)
{

    try
    {
        WebRequest request = WebRequest.Create(oS.fullUrl);

        if (oS != null && oS.oRequest.headers != null && oS.oRequest.headers.Count() > 0)
        {
            NameValueCollection coll = new NameValueCollection();
            request.Headers = new WebHeaderCollection();
            foreach (Fiddler.HTTPHeaderItem rh in oS.oRequest.headers)
            {
                if (rh.Name.Contains("Cookie"))
                {
                    ((HttpWebRequest)request).CookieContainer = new CookieContainer();
                    string[] cookies = UtilitiesScreenScrapper.UtilityMethods.SplitString(rh.Value, ";");
                    if (cookies != null && cookies.Length > 0)
                    {
                        foreach (string c in cookies)
                        {
                            string[] cookie = UtilitiesScreenScrapper.UtilityMethods.SplitString(c, "=");
                            if (cookie != null && cookie.Length > 0)
                            {
                                cookie[0] = cookie[0].Replace(" ", "%");
                                cookie[1] = cookie[1].Replace(" ", "%");

                                ((HttpWebRequest)request).CookieContainer.Add(new Uri(oS.fullUrl), new Cookie(cookie[0].Trim(), cookie[1].Trim()));
                            }
                        }
                    }
                    else
                    {
                        string[] cookie = UtilitiesScreenScrapper.UtilityMethods.SplitString(rh.Value, "=");
                        if (cookie != null && cookie.Length > 0)
                        {
                            ((HttpWebRequest)request).CookieContainer.Add(new Uri(oS.url), new Cookie(cookie[0], cookie[1]));
                        }
                    }

                }

                else if (rh.Name.Contains("User-Agent"))
                {
                    ((HttpWebRequest)request).UserAgent = rh.Value;
                }
                else if (rh.Name.Contains("Host"))
                {
                    ((HttpWebRequest)request).Host = "www." + oS.host;
                }
                else if (rh.Name.Equals("Accept"))
                {
                    ((HttpWebRequest)request).Accept = rh.Value;
                }
                else if (rh.Name.Contains("Content-Type"))
                {
                    ((HttpWebRequest)request).ContentType = rh.Value;
                }
                else if (rh.Name.Contains("Content-Length"))
                {
                    ((HttpWebRequest)request).ContentLength = oS.RequestBody.Length;
                }
                else if (rh.Name.Contains("Connection"))
                {
                    //((HttpWebRequest)request).Connection = rh.Value;

                }
                else if (rh.Name.Equals("Referer"))
                {
                    ((HttpWebRequest)request).Referer = oS.host;
                }
                else
                {

                    ((HttpWebRequest)request).Headers.Add(rh.Name + ":");
                    ((HttpWebRequest)request).Headers[rh.Name] = rh.Value;

                }

            }
            ((HttpWebRequest)request).Headers.Add("Conneciton:");
            ((HttpWebRequest)request).Headers["Conneciton"] = "keep-alive";
            ((HttpWebRequest)request).AllowAutoRedirect = true;


            Stream dataStream;
            if (oS.RequestBody.Length > 0)
            {
                request.Method = "POST";
                // Get the request stream.
                dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(oS.RequestBody, 0, oS.RequestBody.Length);
                // Close the Stream object.
                dataStream.Close();
            }
            else
            {
                request.Method = "GET";
            }
            //string postData = string.Empty;
            //byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            //request.ContentType = "application/x-www-form-urlencoded";

            // Get the response.
            WebResponse response = request.GetResponse();
            //resp = response;
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            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);
            htmlContent = responseFromServer;
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();

        }
    }
    catch(Exception ex)
    {
        throw ex;
    }
    return false;

}