2

ログイン プログラムで問題が発生しました。一度ログインすると、CookieContainer と CookieCollection が (http クラスとともに) null になり、再度ログインしようとすると、最初のリクエストから Cookie が送信されます。なぜクッキーがくっつくのですか?

例:

HTTP uh;

MainWindow()
{
    uh = new HTTP(); 
    uh.login("mySite"); 
    //Logging in....
    //Login Successful.....

    uh.cookieContainer = null;
    uh.cookieCollection = null;
    uh = null; 
    uh = new HTTP(); 
    uh.loging("mySite");
    //Logging in, with cookies from first login
    //Login Fails.....

}

編集: HTTP クラスの大まかな表現...

public class HTTP()
{   
    public CookieContainer cookieContainer;
    public CookieCollection cookieCollection;
    private HttpWebRequest Request;
    private HttpWebResponse Response;

    public HTTP()
    {
        this.cookieContainer = new CookieContainer();
        this.cookieCollection = new CookieCollection();
    }

    public HttpWebResponse login(string url)
    {
        string[] cred = new string[2] { "un", "pw" };

        this.Request = (HttpWebRequest)HttpWebRequest.Create(url);
        this.Request.CookieContainer = cookieContainer;
        byte[] ByteArray = Encoding.UTF8.GetBytes(String.Format("un={0}&pw={1}", cred));
        this.Request.ContentLength = ByteArray.Length;
        Stream DataStream = this.Request.GetRequestStream();
        DataStream.Write(ByteArray, 0, ByteArray.Length);
        DataStream.Close();
        Response = (HttpWebResponse)this.Request.GetResponse();
        this.cookieCollection = Response.Cookies;

        return Response; 
    }

    public bool responseHandle(HttpWebResponse r) 
    {
        //Determines success, logs headers, html body, etc..
    }
}

編集 | 解決策: 上記のコードに問題はありませんでした。HTTPログアウト ボタンに null/new コードを入力しないというばかげた間違いを犯しました。そのため、リセットされることはありませんでした。みんなの時間を無駄にしてごめんなさい。

4

1 に答える 1

2

使い終わったら Cookie を期限切れにすると、Cookie は消えます。(別名、有効期限を経過時間に設定します)。

于 2011-03-18T15:22:22.917 に答える