title: Cannot login website with httprequest POST in C#?
body: <p>I'm trying to write a bit of code to login to a website. But it's not working. Please can you give me some advice. This is my a bit of code:</p>
  
  <pre><code>static void Main(string[] args)
      {
          CookieContainer container = new CookieContainer();
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://pagehype.com/login.php");
          request.Method = "POST";
          request.Timeout = 10000;
          request.ReadWriteTimeout = 30000;
          request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) (Prevx 3.0.5)";
          request.CookieContainer = container;
  
          ASCIIEncoding encoding = new ASCIIEncoding();
          string postData = "username=user&amp;password=password&amp;processlogin=1&amp;return=";
          byte[] data = encoding.GetBytes(postData);
  
          request.ContentType = "application/x-www-form-urlencoded";
          request.ContentLength = data.Length;
          Stream newStream = request.GetRequestStream();
          newStream.Write(data, 0, data.Length);
          newStream.Close();
  
          HttpWebResponse response = (HttpWebResponse)request.GetResponse();
          StreamReader reader = new StreamReader(response.GetResponseStream());
          string htmldoc = reader.ReadToEnd();
          response.Close();
  
          Console.Write(htmldoc);
      }
  </code></pre>
  
  <p>Many thanks,</p>