2

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.

PiZzL3
  • 2,092
  • 4
  • 22
  • 30

1 Answers1

2

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

Serapth
  • 7,122
  • 4
  • 31
  • 39
  • That may work... but it seems hackish... No offense.. I honestly don't like that possible solution. Plus it could cause other errors depending on how I make my requests later on. – PiZzL3 Mar 18 '11 at 15:51
  • Thanks for the help, I found out what was wrong and posted it above. However, because you answered my question with a technically correct method, I'm accepting it as the answer. Thank you got your time and help! – PiZzL3 Mar 18 '11 at 17:13