title: HttpWebRequest chunked/async POST
body: <p>Hi I want to upload some dynamic generated content to my web api.
  On the client I use the HttpWebRequest. The data should be uploaded sync and I want to write to the stream AFTER(!) I executed the HTTP-request.</p>
  
  <p>(From server to client it does work fine, but from client to server i get some exceptions).</p>
  
  <p>The client implementation looks like:</p>
  
  <pre><code>HttpWebRequest httpWebRequest = HttpWebRequest.Create(myUrl) as HttpWebRequest;
  httpWebRequest.Method = "POST";
  httpWebRequest.Headers["Authorization"] = "Basic " + ... ;
  httpWebRequest.PreAuthenticate = true;
  
  httpWebRequest.SendChunked = true;
  //httpWebRequest.AllowWriteStreamBuffering = false; //does not help...
  httpWebRequest.ContentType = "application/octet-stream";
  Stream st = httpWebRequest.GetRequestStream();
  
  Task&lt;WebResponse&gt; response = httpWebRequest.GetResponseAsync();
  
  // NOW: Write after GetResponse()
  
  var b = Encoding.UTF8.GetBytes("Test1");
  st.Write(b, 0, b.Length);
  
  b = Encoding.UTF8.GetBytes("Test2");
  st.Write(b, 0, b.Length);
  
  b = Encoding.UTF8.GetBytes("Test3");
  st.Write(b, 0, b.Length);
  
  st.Close();
  
  var x = response.Result;
  Stream resultStream = x.GetResponseStream();
  //do some output...
  </code></pre>
  
  <p>I get exceptions (NotSupportedException: The stream does not support concurrent IO read or write operations.) on stream.write().</p>
  
  <p>Why do I get here an exceptions. Some times the first writes worke and the late writes throw the exception.
  On the beginning the stream.CanWrite property is true, but after the first or second or thrird write it goes false... And then the exception is thrown on the next write.</p>
  
  <p>Edit: Changing AllowWriteStreamBuffering did not help</p>
  
  <p><strong>Appendix:</strong>
  <em>I found my problem.</em> This problem is caused by the order of my code.
  I have to call it in this order:</p>
  
  <ul>
  <li>GetRequestStream
  (writing async to  the stream)
  (the request is send to the server after the first write)
  then:</li>
  <li>GetResponseAsync()</li>
  <li>GetResponseStream()</li>
  </ul>
  
  <p>I thought "GetResponseAsync" triggers the client to send the Request (for now the headers only).
  But it is not necessary because the request is already send after I write the first bits to the stream.</p>
  
  <p>Second cause of my problems: Fiddler. (Fiddler currently only supports streaming of responses and not requests)</p>