【问题标题】:Handeling Cookies With the Broswer Control in C#在 C# 中使用浏览器控件处理 Cookie
【发布时间】:2012-10-11 09:31:39
【问题描述】:
我正在自动化网站上的流程。为简单起见,假设该过程有两个部分。第一个是登录网站,第二个是单击页面上的按钮。我相信登录机制使用 cookie 来处理身份验证。我用过 Fiddler 并且能够查看这个 cookie。
我的问题是,到目前为止,我可以自动登录并单击按钮,但我只能为一个控件执行此操作。我只有一次登录,系统不允许我使用其他浏览器再次登录。我想要做的是发出多个请求以同时单击按钮。但现在我被困在按顺序执行它们。
有没有一种方法可以让我从浏览器控件中获取 cookie 并将其用于其他网络请求?
【问题讨论】:
标签:
c#
automation
httpwebrequest
webbrowser-control
【解决方案1】:
您可以使用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;
}