13
  1. 在整个响应流回之前,如何访问响应标头?
  2. 我如何在流到达时读取它?
  3. 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 正在进行,哪些将导致等待直到任务完成。

4

2 回答 2

7

根据我自己的测试,在您开始读取内容流之前不会传输内容,并且您正确地认为调用Task.Result是阻塞调用,但它的本质是同步点。但是,它不会阻塞预缓冲整个内容,它只会阻塞直到内容开始来自服务器。

因此,无限流不会阻塞无限长的时间。因此,尝试异步获取流可能被认为是矫枉过正,尤其是在您的标头处理操作相对较短的情况下。但是,如果您愿意,您始终可以在处理另一个任务的内容流时处理标头。像这样的东西可以做到这一点。

static void Main(string[] args)
{
    var url = "http://somesite.com/bigdownloadfile.zip";
    var client = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Get, url);

    var getTask = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
    Task contentDownloadTask = null;

    var continuation = getTask.ContinueWith((t) =>
    {
        contentDownloadTask = Task.Run(() =>
        {
            var resultStream = t.Result.Content.ReadAsStreamAsync().Result;
            resultStream.CopyTo(File.Create("output.dat"));
        });

        Console.WriteLine("Got {0} headers", t.Result.Headers.Count());
        Console.WriteLine("Blocking after fetching headers, press any key to continue...");
        Console.ReadKey(true);
    });

    continuation.Wait();
    contentDownloadTask.Wait();
    Console.WriteLine("Finished downloading {0} bytes", new FileInfo("output.dat").Length);

    Console.WriteLine("Finished, press any key to exit");
    Console.ReadKey(true);
}

请注意,无需检查标头部分是否完整,您已使用HttpCompletionOption.ResponseHeadersRead选项明确指定了这一点。SendAsync在检索到标头之前,该任务将不会继续。

于 2013-03-13T01:35:14.450 回答
6

使用 await/async 关键字的结果更具可读性:

var url = "http://somesite.com/bigdownloadfile.zip";

using (var httpClient = new HttpClient())
using (var httpRequest = new HttpRequestMessage(HttpMethod.Get, url ))
using(HttpResponseMessage response = await httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead))
using (Stream stream = await response.Content.ReadAsStreamAsync())
{
    //Access to the Stream object as it comes, buffer it or do whatever you need
}    
于 2013-10-18T14:06:59.977 回答