title: Reading "chunked" response with HttpWebResponse
body: <p>I'm having trouble reading a "chunked" response when using a StreamReader to read the stream returned by GetResponseStream() of a HttpWebResponse:</p>
  
  <pre><code>// response is an HttpWebResponse
  StreamReader reader = new StreamReader(response.GetResponseStream());
  string output = reader.ReadToEnd(); // throws exception...
  </code></pre>
  
  <p>When the <code>reader.ReadToEnd()</code> method is called I'm getting the following System.IO.IOException: <strong>Unable to read data from the transport connection: The connection was closed.</strong></p>
  
  <p>The above code works just fine when server returns a "non-chunked" response.</p>
  
  <p>The only way I've been able to get it to work is to use HTTP/1.0 for the initial request (instead of HTTP/1.1, the default) but this seems like a lame work-around.</p>
  
  <p>Any ideas?</p>
  
  <hr>
  
  <p>@Chuck</p>
  
  <p>Your solution works pretty good. It still throws the same IOExeception on the last Read(). But after inspecting the contents of the StringBuilder it looks like all the data has been received. So perhaps I just need to wrap the Read() in a try-catch and swallow the "error".</p>