2

我的登录程序遇到了问题。如果我登录一次,然后为 null CookieContainer 和 CookieCollection(以及我的 http 类),然后尝试再次登录,它仍然会从第一个请求中提交 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空/新代码放在我的注销按钮中。所以它从来没有重置过。很抱歉浪费了大家的时间。

4

1 回答 1

2

使用完 cookie 后过期,它就会消失。(又名,将其过期日期设置为经过的时间)。

于 2011-03-18T15:22:22.917 回答