title: C# HttpWebRequest CookieContainer/Collection persisting between Object instances?
body: <p>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? </p>
  
  <p>Example: </p>
  
  <pre><code>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.....
  
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>  Rough Representation of the HTTP class...</p>
  
  <pre><code>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}&amp;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..
      }
  }
  </code></pre>
  
  <p><strong>EDIT | SOLUTION:</strong>
  There was no problem with the any code above. I made the dumb mistake of not putting the <code>HTTP</code> null/new code in my logout button. So it was never reseting. Sorry for wasting everyones time. </p>