title: Saving login Cookie and use it for other request
body: <p>I'm new to c#. I'm trying to login to a website, using a c# post request. </p>
  
  <p>Does this code save the cookie actually to the CookieContainer and would it let me use this cookie in other requests also? How would I for example post a get request now with the cookie I saved from the login?</p>
  
  <p>My main code:</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      try
      {
          string userName = textBox1.Text;
          string passWord = textBox2.Text;
          string postData = "username=" + userName + "&amp;password=" + passWord;
          string requestUrl = "http://registration.zwinky.com/registration/loginAjax.jhtml";
  
          post botLogin = new post();
          botLogin.postToServer (postData ,requestUrl);
      }
      catch (Exception ex)
      {
          MessageBox.Show("Error :" + ex.Message);
      }
  }
  </code></pre>
  
  <p>My post class:</p>
  
  <pre><code>public class post
  {
      public void postToServer(string postData, string requestUrl)
      {
          HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
          myHttpWebRequest.Method = "POST";
  
          byte[] data = Encoding.ASCII.GetBytes(postData);
  
          myHttpWebRequest.CookieContainer = new CookieContainer();
  
          myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
          myHttpWebRequest.ContentLength = data.Length;
  
          Stream requestStream = myHttpWebRequest.GetRequestStream();
          requestStream.Write(data, 0, data.Length);
          requestStream.Close();
  
          HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
  
          Stream responseStream = myHttpWebResponse.GetResponseStream();
          StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
  
          string pageContent = myStreamReader.ReadToEnd();
  
          myStreamReader.Close();
          responseStream.Close();
  
          myHttpWebResponse.Close();
          MessageBox.Show(pageContent);
      }
  }
  </code></pre>