我正在创建一个应用程序,在登录部分的网站上执行一些不同的操作。
为此,我必须为我提出的所有请求维护一个连续的Cookie会话。
到目前为止,我已经通过HttpWebRequest成功连接到该网站,并且响应已经确认了这一点,但是我无法重复使用该cookie。
我已经阅读了所有SO并找到了指示如何在同一个函数或类中使用cookie的主题,但我需要能够在多个不同的函数中使用cookie。
我的第一个想法是尝试从初始登录函数返回cookie容器,然后将其作为参数传递给每个后续函数,但我无法理解它。
有人能够提出更好的方法或方法吗?
答案 0 :(得分:0)
在网上阅读之后,我终于遇到了解决方案。看来这是我第一次尝试时无效的原因是由于我配置错误的其他设置。
因此,对于可能遇到同样问题的其他人,希望这会有所帮助:
public const string userAgent = "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";
public CookieContainer cookieJar;
private void Login_Click(object sender, EventArgs e)
{
cookieJar = Login();
}
private CookieContainer Login()
{
string username = txtUsername.Text;
string password = txtPassword.Text;
// Create a request using the provied URL.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginPageURL);
// Set the Method property of the request to POST.
request.Method = "POST";
// Set the Cookiecontainer
CookieContainer cookieJar = new CookieContainer();
request.CookieContainer = cookieJar;
// Create POST data and convert it to a byte array.
string postData = "username=" + username + "&password=" + password;
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the User Agent
request.UserAgent = userAgent;
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 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 ();
string cookie = cookieJar.GetCookieHeader(request.RequestUri);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
return cookieJar;
}
private void viewPage(CookieContainer cookieJar, string pageURL)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pageURL);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the Method property of the request to POST.
request.Method = "POST";
// Set the User Agent
request.UserAgent = userAgent;
// Put session back into CookieContainer
request.CookieContainer = cookieJar;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Close the Stream object.
dataStream.Close();
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// 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();
}