C# 在C中使用Broswer控件处理Cookies

C# 在C中使用Broswer控件处理Cookies,c#,automation,httpwebrequest,webbrowser-control,C#,Automation,Httpwebrequest,Webbrowser Control,我正在网站上自动化一个过程。为了简单起见,让我们假设这个过程有两个部分。第一个是登录站点,第二个是单击页面上的按钮。我相信登录机制使用cookie来处理身份验证。我使用了Fiddler,可以查看这个cookie 我的问题是,到目前为止,我可以自动登录并单击按钮,但我只能为一个控件执行此操作。我只有一次登录,系统不允许我使用其他浏览器再次登录。我想做的是发出多个请求以同时单击按钮。但现在我被困在按顺序做这些事情 有没有办法从浏览器控件获取cookie并将其用于其他web请求?您可以使用HttpRe

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

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


有没有办法从浏览器控件获取cookie并将其用于其他web请求?

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

下面的函数可能会对此有所帮助。通过使用此功能,您无需反复登录,只需向请求中添加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;

}