title: C# WebRequest using Cookies
body: <p>I have a winforms application i have been working on that runs multiple tests on consumer accounts. The tests require a one time login in order to execute.</p>
  
  <pre><code>string paramaters = "authmethod=on&amp;chkRememberMe=on&amp;login-form-type=pwd&amp;password=" + pw.Text + "&amp;userid=" + uid.Text + "&amp;username=" + uid.Text;
          string strResponse;
          HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://www.url.com/login.form");
          requestLogin.Method = "POST";
          requestLogin.CookieContainer = cookieJar;
          requestLogin.ContentType = "application/x-www-form-urlencoded";
  
          requestLogin.ContentLength = paramaters.Length;
          StreamWriter stOut = new StreamWriter(requestLogin.GetRequestStream(), System.Text.Encoding.ASCII);
          stOut.Write(paramaters);
          stOut.Close();
  
          StreamReader stIn = new StreamReader(requestLogin.GetResponse().GetResponseStream());
          strResponse = stIn.ReadToEnd();
          stIn.Close();
  </code></pre>
  
  <p>This script works for the login just fine, the problem is when i need to actually run the tests i need to return all the results into a string (HTML results). </p>
  
  <pre><code>private string runTestRequest(Uri url, string parameters)
      {
          string testResults = string.Empty;
          HttpWebRequest runTest = (HttpWebRequest)WebRequest.Create(url);
          runTest.CookieContainer = cookieJar;
          runTest.Method = "POST";
          runTest.ContentType = "application/x-www-form-urlencoded";
          StreamWriter stOut = new StreamWriter(runTest.GetRequestStream(), System.Text.Encoding.ASCII);
          stOut.Write(parameters);
          stOut.Close();
          StreamReader stIn = new StreamReader(runTest.GetResponse().GetResponseStream());
          testResults = stIn.ReadToEnd();
          stIn.Close();
          return testResults;
      }
  </code></pre>
  
  <p>But it goes and tries to login. How can i use the Cookie from the previous login request with this + many other web requests?</p>
  
  <p>Thanks for the help.</p>
  
  <p>EDIT:</p>
  
  <p>I added this to my code yet which should do the same thing as BrokenGlass is saying except just a little different but still doesn't work.</p>
  
  <pre><code>foreach (Cookie cookie in responseLogin.Cookies)
          {
              cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
              richTextBox2.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
          }
  </code></pre>