首页 > 解决方案 > 使用 HttpCompletionOption.ResponseHeadersRead 的 HttpClient 超时

问题描述

Windows 上的 .NET Core 3.1 控制台应用程序,我试图弄清楚为什么在使用HttpCompletionOption.ResponseHeadersReadhttpClient.Timeout后获取内容时似乎不起作用

static async Task Main(string[] args)
{
    var httpClient = new HttpClient();

    // if using HttpCompletionOption this timeout doesn't work
    httpClient.Timeout = TimeSpan.FromSeconds(5);

    var uri = new Uri("http://brokenlinkcheckerchecker.com/files/200MB.zip");

    // will not timeout
    //using var httpResponseMessage = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);

    // will timeout after 5s with a TaskCanceledException
    var httpResponseMessage = await httpClient.GetAsync(uri);

    Console.WriteLine($"Status code is {httpResponseMessage.StatusCode}. Press any key to get content");
    Console.ReadLine();
    Console.WriteLine("getting content");

    var html = await httpResponseMessage.Content.ReadAsStringAsync();
    Console.WriteLine($"finished and length is {html.Length}");
}

也试过一个CancellationToken

// will not timeout
var cts = new CancellationTokenSource(5000);
using var httpResponseMessage = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead,
 cts.Token);

ReadAsStreamAsync

// will not timeout
using (Stream streamToReadFrom = await httpResponseMessage.Content.ReadAsStreamAsync())
{
    string fileToWriteTo = Path.GetTempFileName();
    using (Stream streamToWriteTo = File.Open(fileToWriteTo, FileMode.Create))
    {
        await streamToReadFrom.CopyToAsync(streamToWriteTo);
    }
}

HttpCompletionOption我从这篇很棒的文章中 了解到: https ://www.stevejgordon.co.uk/using-httpcompletionoption-responseheadersread-to-improve-httpclient-performance-dotnet

更新 使用下面的@StephenCleary 答案将cancellationToken 传递到CopyToAsync现在可以按预期工作的方法中。

我在下面包含了更新的代码,它显示了将 a MemoryStreamthen 复制到一个字符串中,我发现很难找到如何去做。对于我的用例,这很好。

string html;
await using (var streamToReadFrom = await httpResponseMessage.Content.ReadAsStreamAsync())
await using (var streamToWriteTo = new MemoryStream())
{
    await streamToReadFrom.CopyToAsync(streamToWriteTo, cts.Token);
    // careful of what encoding - read from incoming MIME
    html = Encoding.UTF8.GetString(streamToWriteTo.ToArray());
}

标签: c#http.net-core

解决方案


我希望HttpClient.Timeout只适用于GetAsync请求的一部分。HttpCompletionOption.ResponseHeadersRead意思是“在读取响应头时考虑Get完整”,所以它是完整的。所以问题在于它不适用于从流中读取。

我建议使用Polly 的 Timeout而不是HttpClient.Timeout; Polly 是一个通用库,可用于使任何操作超时。

如果此时不想使用 Polly,可以将 传递CancellationTokenStream.CopyToAsync


推荐阅读