使用 HttpCompletionOption.ResponseHeadersRead 的 HttpClient 超时

Dav*_*eer 5 c# http .net-core

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}");
}

Run Code Online (Sandbox Code Playgroud)

也尝试过一个CancellationToken

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

Run Code Online (Sandbox Code Playgroud)

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);
    }
}

Run Code Online (Sandbox Code Playgroud)

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

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

我在下面包含了更新的代码,其中显示了复制到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());
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*ary 4

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

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

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