private void ReadResponseBody(ByteStreamReader reader, HttpResponseMessage response) { HttpContent content = null; if (response.Headers.TransferEncodingChunked.GetValueOrDefault(false)) { // read the body with chunked transfer encoding var remainingStream = reader.GetRemainingStream(); var chunkedStream = new ReadsFromChunksStream(remainingStream); content = new StreamContent(chunkedStream); } else if (response.Content.Headers.ContentLength.HasValue) { // read the body with a content-length var remainingStream = reader.GetRemainingStream(); var limitedStream = new LimitedStream(remainingStream, response.Content.Headers.ContentLength.Value); content = new StreamContent(limitedStream); } else { // TODO: should we immediately close the connection in this case? } if (content != null) { // copy over the content headers foreach (var header in response.Content.Headers) { content.Headers.TryAddWithoutValidation(header.Key, header.Value); } response.Content = content; } }