title: Login to Https Website with WebRequest and WebResponse Not Working C#
body: <p>I am working on a program that will log into a website and get certian data. However I am having trouble posting the login parameters and dealing with the cookies, as each time I get a page saying "You have logged out or Session has expired." So clearly I'm doing something wrong with posting the parameters or dealing with the cookies, but don't know which. I have been working on this for a while and just can't get my head around why this is not working correctly.    </p>
  
  <pre><code>void Login2(string username, string password)
      {
          string pageSource;
          string formUrl = "https://forUrl.com";
          string formParams = string.Format("login={0}&amp;sslProt={1}&amp;pwd={2}&amp;gru={3}", username, "", password, "115237091");
          string cookieHeader;
  
          HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
          req.AllowAutoRedirect = false;
          req.ContentType = "application/x-www-form-urlencoded";
          req.Method = "POST";
          byte[] bytes = System.Text.Encoding.ASCII.GetBytes(formParams);
          req.ContentLength = bytes.Length;
          using (Stream os = req.GetRequestStream())
          {
              os.Write(bytes, 0, bytes.Length);
          }
          HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
          cookieHeader = resp.Headers["Set-cookie"];
  
          string getUrl = "https://Urlbehindform.com";   
          HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
          getRequest.Method = "GET";
          getRequest.AllowAutoRedirect = false;
          getRequest.Headers.Add("Cookie", cookieHeader);
          HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
          using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
          {
              pageSource = sr.ReadToEnd();
          }
          Response.Redirect(getUrl);
  
      }
  </code></pre>
  
  <p>I am getting the cookie when I do the POST and sending it back when I do the GET, but for some reason this doesn't seem to work. At first I thought it was the parameters, but after looking at the issue further using Tamper Data with Firefox the login parameters seem to be working fine. Any help would be great, as I have been working on this for a while and can't wrap my head around it. Thanks!</p>
  
  <p><em>UPDATE</em>:</p>
  
  <p>After trying out a few suggestions I still can't get this to work. However Upon looking deeper into Data Tamper, It appears that there is a POST with the login parameters, then a GET to a different page and then finally the GET to the page after the login page (The one I'm trying to get to). After some further debugging I actually discovered that my login POST is not working as I thought, As the response header location is showing "/cv/scripts/A028/eng/logErr.asp". Meaning the rest of my code could have been fine all a long, it was that the POST wasn't giving me a valid login. Any Sugguestions as to why I am always getting the login error page? As always thanks for the help.</p>
  
  <p><em>UPDATE</em>:</p>
  
  <p>After playing around further with Tamper Data is appears that the reason I am unable to get a successful login is that in order have a successful POST of the parameters there needs to be a cookie already obtained. How do I go about doing this?    </p>