我正在创建一个应用程序,以在登录部分的网站上执行一些不同的事情。

为此,我必须为所有请求维护一个连续的cookie会话。

到目前为止,我已经通过httpwebrequest成功连接到该网站,并且回复已经确认了这一点,但是我无法重复使用cookie。

我已经阅读了整个内容,并找到了指示如何在相同功能或类中使用cookie的主题,但是我需要能够在多个不同功能中使用cookie。

我的第一个想法是尝试从初始登录函数返回cookie容器,然后将其作为参数传递给每个后续函数,但我只是无法实现。

有人能够提出一种更好的方法还是我可以实现这一目标的方法?

有帮助吗?

解决方案

在网上大量阅读后,我最终遇到了解决方案。看来,我第一次尝试时没有工作的原因是由于我配置了错误的其他设置。

因此,对于可能遇到同样问题的其他任何人,希望这会有所帮助:

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();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top