异步读取分块与HttpClient的内容从ASP.NET的WebAPI [英] Async reading chunked content with HttpClient from ASP.NET WebApi

查看:588
本文介绍了异步读取分块与HttpClient的内容从ASP.NET的WebAPI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用HttpClient的阅读分块(在HTTP 1.1块传输编码的意义上)异步内容。

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

我在看HttpContent在异步方法:
MSDN链接

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

我得到的分块内容precisely因为服务器不知道提前时将所有的数据是可用的,所以我不知道什么时候将所有的内容到达。而不是等待,可能是几个小时,为完成任务,我希望能够为他们到达读取数据块。

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.

我可以以某种方式读取响应内容的一部分,像有一些任务时响应收到每个4K字节的内容,将完成?

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?

时使用的HttpClient有利的所有在这种情况下?

Is using HttpClient advantageous at all in this case?

推荐答案

使用HttpClient.SendAsync你可以传递一个HttpCompletionOption参数告诉HttpClient的不来缓冲对你的反应,并尽快返回,因为它得到了头。然后你可以使用ReadAsStreamAsync得到一个流,让你拉数据到达。

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.

这篇关于异步读取分块与HttpClient的内容从ASP.NET的WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆