title: HttpWebRequest POST gives 500 server error
body: <p>I need to make from my app an authentificated httpwebrequest. the response to my request should be in json format. for this i'm using the code below:</p>
  
  <pre><code>// Create the web request 
          Uri address = new Uri("http://www.mysite.com/remote/user/login/format/json");
          HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
          request.Method = "POST";
          request.UseDefaultCredentials = false;
          request.Credentials = new NetworkCredential(UserName, Password);
          request.PreAuthenticate = true;
          request.ContentType = "application/x-www-form-urlencoded";
          request.Accept = "application/json";
  
          string data = string.Format("username={0}&amp;password={1}", otherusername, otherpassword);
          // Create a byte array of the data we want to send  
          byte[] byteData = UTF8Encoding.UTF8.GetBytes(data);
  
          // Set the content length in the request headers  
          request.ContentLength = byteData.Length;
  
           //Write data  
          using (Stream postStream = request.GetRequestStream())
          {
              postStream.Write(byteData, 0, byteData.Length);
          }
  
  
          // Get response
          try
          {
              using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
              {
                  // Get the response stream  
                  StreamReader reader = new StreamReader(response.GetResponseStream());
  
                  // Console application output  
                  jsonResponse = reader.ReadToEnd();
                  reader.Close();
              }
  
              user = new User();
              JObject o = JObject.Parse(jsonResponse);
              user.Unguessable_id = (string)o["unguessable_id"];
              user.Print_id = (string)o["print_id"];
              user.Rrid = (string)o["rrid"];
              user.Raid = (string)o["raid"];
  
          }
          catch (WebException ex) {
              errorMessage = ex.Message;
          }
  </code></pre>
  
  <p>the problem is that the very first call it always gives a 500 error on the server. and the request fails. if i redo the call(by making an refresh in my browser) the request is successful.</p>
  
  <p>the request should look like this in normal conditions:</p>
  
  <pre><code>POST /remote/user/login/format/json HTTP/1.1
  Host: &lt;yourhost&gt;
  
  username=user&amp;password=pass
  </code></pre>
  
  <p>but when the server sends out the 500 error he received something like this:</p>
  
  <pre><code>username=user&amp;password=passwordPOST /remote/user/login/format/json HTTP/1.1
  </code></pre>
  
  <p>any idea why this is happening? in my test app if i refresh the page that makes the httpwebrequest the call is successful.</p>
  
  <p><strong>EDIT:</strong>
  after installing Fiddler the requests made look like this:</p>
  
  <p>=> the one that generates 500</p>
  
  <pre><code>POST http://www.mysite.com/remote/user/login/format/json HTTP/1.1
  Content-Type: application/x-www-form-urlencoded
  Accept: application/json
  Host: www.mysite.com
  Content-Length: 30
  Expect: 100-continue
  Connection: Keep-Alive
  
  username=user&amp;password=pass
  </code></pre>
  
  <p>=> the one made on refresh</p>
  
  <pre><code>POST http://www.mysite.com/remote/user/login/format/json HTTP/1.1
  Content-Type: application/x-www-form-urlencoded
  Accept: application/json
  Authorization: Basic ZGNpOkFpR2g3YWVj
  Host: www.mysite.com
  Content-Length: 30
  Expect: 100-continue
  Connection: Keep-Alive
  
  username=user&amp;password=pass
  </code></pre>
  
  <p>it seems that <strong>Authorization: Basic ZGNpOkFpR2g3YWVj</strong> is not included in the first request...why is that happening?(i'm using the same code for both requests)</p>