Async reading chunked content with HttpClient from

2019-02-13 06:52发布

问题:

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?

回答1:

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.