我正在创建一个应用程序,以便在登录部分的网站上做一些不同的事情。
为此,我必须为我发出的所有请求维护一个连续的Cookie会话。
到目前为止,我已经成功地通过HttpWebRequest连接到了网站,响应也证实了这一点,但是我一直无法重复使用cookie。
我通读了所有的内容,找到了一些主题,指出了如何在相同的函数或类中使用cookie,但我需要能够在多个不同的函数中使用cookie。
我的第一个想法是尝试从初始登录函数返回cookie容器,然后将其作为参数传递给每个后续函数,但我就是无法让它继续运行。
有没有人能提出一个更好的方法或者我能做到这一点的方法?
发布于 2012-02-18 16:44:08
经过大量的在线阅读,我最终自己找到了解决方案。这似乎是因为我第一次尝试它时没有工作的原因,因为我配置了错误的其他设置。
因此,对于其他可能遇到同样问题的人,希望这能有所帮助:
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();
}https://stackoverflow.com/questions/9305985
复制相似问题