title: C# - HttpWebRequest - POST
body: <p>I am trying to make an Http POST to an Apache web server.</p>
  
  <p>I am finding that setting ContentLength seems to be required for the request to work.</p>
  
  <p>I would rather create an XmlWriter directly from GetRequestStream() and set SendChunked to true, but the request hangs indefinitely when doing so.</p>
  
  <p>Here is how my request is created:</p>
  
  <pre><code>    private HttpWebRequest MakeRequest(string url, string method)
      {
          HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
          request.Method = method;
          request.Timeout = Timeout; //Property in my class, assume it's 10000
          request.ContentType = "text/xml"; //I am only writing xml with XmlWriter
          if (method != WebRequestMethods.Http.Get)
          {
              request.SendChunked = true;
          }
          return request;
      }
  </code></pre>
  
  <p>How can I make SendChunked work so I do not have to set ContentLength?  I do not see a reason to store the XmlWriter's string somewhere before sending it to the server.</p>
  
  <p>EDIT: Here is my code causing the problem:</p>
  
  <pre><code>    using (Stream stream = webRequest.GetRequestStream())
      {
          using (XmlWriter writer = XmlWriter.Create(stream, XmlTags.Settings))
          {
              Generator.WriteXml&lt;TRequest&gt;(request, writer);
          }
      }
  </code></pre>
  
  <p>Before I did not have a using on the Stream object returned from GetRequestStream(), I assumed XmlWriter closed the stream when disposed, but this is not the case.</p>
  
  <p>One of the answers below, let me to this.  I'll mark them as the answer.</p>
  
  <p>As far as HttpWebRequest is concerned, my original code works just fine.</p>