title: C# login to website using HTTPWebRequest
body: <p>I am trying to login to this website: <a href="https://lms.nust.edu.pk/portal/login/index.php" rel="nofollow">https://lms.nust.edu.pk/portal/login/index.php</a>
  Here's my code:</p>
  
  <pre><code>   const string uri = "https://lms.nust.edu.pk/portal/login/index.php";
      HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(uri);
      wr.KeepAlive = true;
      wr.Method = "POST";
      wr.AllowAutoRedirect = false;
      wr.Proxy = p;
      wr.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
      wr.ContentType = "application/x-www-form-urlencoded";
      wr.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36";
      string pdata = "username=USERNAME&amp;password=PASSWORD";
      byte[] data = UTF8Encoding.UTF8.GetBytes(pdata);
      wr.ContentLength = data.Length;
      CookieContainer cookie = new CookieContainer();
      wr.CookieContainer = cookie;
      using (Stream poststream = wr.GetRequestStream())
      {
          poststream.Write(data, 0, data.Length);
      }
      HttpWebResponse wp = (HttpWebResponse)wr.GetResponse();
      wr.CookieContainer.Add(wp.Cookies);
  </code></pre>
  
  <p>Its not going past the login page. Here's what I know is happening. There are no cookies in the <code>wp.Cookies</code>. I debugged the code, which told me that even though I have the uri specified and have set <code>AllowAutoRedirect=false</code>, its not going to that uri and another url. I don't know why or how.
  Can somebody help me?</p>