- 在整个响应流返回之前,我如何访问响应头?
- 我如何在数据流到达时读取数据流?
- HttpClient是我接收HTTP响应的最佳选择吗?
以下是一段可能说明我的问题的代码片段:
using (var response = await _httpClient.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead))
{
var streamTask = response.Content.ReadAsStreamAsync();
//how do I check if headers portion has completed?
//Does HttpCompletionOption.ResponseHeadersRead guarantee that?
//pseudocode
while (!(all headers have been received))
//maybe await a Delay here to let Headers get fully populated
access_headers_without_causing_entire_response_to_be_received
//how do I access the response, without causing an await until contents downloaded?
//pseudocode
while (stremTask.Resul.?) //i.e. while something is still streaming
//? what goes here? a chunk-read into a buffer? or line-by-line since it's http?
...
编辑以澄清我另一个模��的领域:
我找到的任何参考资料都有某种阻止语句,这会导致等待内容到达。 我读过的参考资料通常访问streamTask.Result或Content上的方法或属性,而我不知道哪些参考资料在streamTask进行时是可行的,哪些会导致等待任务完成。
streamTask.Result
会阻塞直到Stream
可用,但它并不要求整个流内容已经传输完成。在调用streamTask.Result
之后,技术上可能没有任何内容字节可用。 - Snixtor