0

ログインセクションのWebサイトでいくつかの異なることを行うアプリケーションを作成しています。

これを行うには、作成するすべてのリクエストに対して 1 つの継続的な Cookie セッションを維持する必要があります。

これまでのところ、HttpWebRequest を介して Web サイトに正常に接続しており、応答でこれが確認されていますが、Cookie を再利用できませんでした。

私はSO全体を読み、同じ関数またはクラス内でCookieを使用する方法を示すトピックを見つけましたが、複数の異なる関数でCookieを使用する機能が必要です.

私が最初に考えたのは、最初のログイン関数から Cookie コンテナーを返して、それを後続の各関数にパラメーターとして渡すことでしたが、うまくいきませんでした。

これを達成するためのより良い方法または方法を提案できる人はいますか?

4

1 に答える 1

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();
}
于 2012-02-18T08:44:08.370 に答える