异步读取分块与HttpClient的内容从ASP.NET的WebAPI

编程入门 行业动态 更新时间:2024-10-25 14:26:11
本文介绍了异步读取分块与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

本文发布于:2023-11-14 04:48:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586264.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内容   HttpClient   ASP   WebAPI   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!