Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async reading chunked content with HttpClient from ASP.NET WebApi

I would like to use HttpClient to read the chunked (in the sense of HTTP 1.1 chunked transfer encoding) content asynchronously.

I am looking at HttpContent async methods at: MSDN link

However, in the case of returned Task for byte array, for example:

The returned Task object will complete after all of the content has been written as a byte array

I am getting chunked content precisely because server doesn't know ahead of time when will all of the data be available, thus I don't know when will all of the content arrive. Rather than waiting, possibly for hours, for the task to complete, I would like to be able to read the chunks as they arrive.

Can I somehow read part of the response content, like have some task that would complete when every 4K bytes of content are received in response?

Is using HttpClient advantageous at all in this case?

like image 895
Tony Avatar asked Sep 21 '12 15:09

Tony


1 Answers

Using HttpClient.SendAsync you can pass a HttpCompletionOption parameter to tell HttpClient not to buffer the response for you and return as soon as it gets the headers. Then you can use ReadAsStreamAsync to get a stream that will allow you to pull the data as it arrives.

like image 181
Darrel Miller Avatar answered Sep 28 '22 11:09

Darrel Miller