문제

I've run into a problem with my Login program. If I login one time, then null CookieContainer and CookieCollection (along with my http class), then try to login again, it still submits the cookies from the first request. Why to the cookies stick around?

Example:

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.....

}

EDIT: Rough Representation of the HTTP class...

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..
    }
}

EDIT | SOLUTION: There was no problem with the any code above. I made the dumb mistake of not putting the HTTP null/new code in my logout button. So it was never reseting. Sorry for wasting everyones time.

도움이 되었습니까?

해결책

Expire the cookie when you are done with it and it will go away. ( AKA, set its expires date to an elapsed time ).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top