title: ContentLength error for Request Stream in C#
body: <p>I'm trying to get some data back from rest WS in C# but I'm getting this error:</p>
  <blockquote>
  <p>You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.</p>
  </blockquote>
  <p>This is the code I'm trying to use:</p>
  <pre><code>var json = new JavaScriptSerializer().Serialize(order);
          string jsonResponseToString = &quot;&quot;;
          HttpWebRequest request =
              (HttpWebRequest)WebRequest.Create(&quot;https://myurl.com&quot;);
          request.Method = &quot;POST&quot;;
          System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
          Byte[] byteArray = encoding.GetBytes(json);
          request.ContentLength = byteArray.Length;
          request.ContentType = @&quot;application/json&quot;;
          try
          {                
              using (var response = (HttpWebResponse)request.GetResponse())
              {
                  Stream str = response.GetResponseStream();
                  var sr = new StreamReader(str, encoding);                    
                  
                  jsonResponseToString = sr.ReadToEnd();
                  var result = new OrderResult();
                  result = new JavaScriptSerializer().Deserialize&lt;OrderResult&gt;(jsonResponseToString);
                  if (str != null)
                  {
                      str.Flush();
                      str.Close();
                  }
              }
          }
          catch (WebException ex)
          {
              // 
          }               
  </code></pre>
  <p>The error is happening on this line:</p>
  <pre><code>using (var response = (HttpWebResponse)request.GetResponse())
  </code></pre>
  <p>How can I solve this, am I doing something wrong?</p>