title: C#: Getting message body from HTTP 409 Conflict response
body: <p>I am trying to get message body from web service (located at <a href="http://www.ekkuli.net/conflict_call.php" rel="nofollow noreferrer">http://www.ekkuli.net/conflict_call.php</a>), which is returning 'Transfer-Encoding: chunked' tag and 409 Conflict response using the following code:</p>
  
  <pre><code>String webAddr = "http://www.ekkuli.net/conflict_call.php";
  var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
  httpWebRequest.ContentType = "application/json; charset=utf-8";
  httpWebRequest.Method = "POST";
  httpWebRequest.ServicePoint.Expect100Continue = false;
  httpWebRequest.Accept = "application/json";
  httpWebRequest.KeepAlive = true;
  
  try {
      using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) {
          String myJson = "{ \"method\" : \"just testing\" }";
          streamWriter.Write(myJson);
          streamWriter.Flush();
       }
  
       var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  
       using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) {
           var responseText = streamReader.ReadToEnd(); 
        }
  } catch (WebException ex) { // Catch 409 Conflict response
      try {
          // Try to get message body from 409 Response
          var s = ex.Response.GetResponseStream();
          var sr = new System.IO.StreamReader(s);
          var content = sr.ReadToEnd(); // THIS THROWS AN EXCEPTION
      } catch (Exception excep) {
          // Exception! Error getting response stream (ReadAsync): ReceiveFailure Value cannot be null. 
      }
  }
  </code></pre>
  
  <p>Is there any way to get response body from 409 Conflict -response, when server is returning 'Transfer-encoding: Chuncked' -header? If this header is disabled and Content-length is returned instead, everything works fine, but in this case I cannot modify the server code.</p>
  
  <p>Same problem occurs, when trying with RestSharp -library (it returns status code 0 and message body 0).</p>
  
  <p>I am using Mono (for Mac).</p>