The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing

2022-03-25  乐帮网

c#

C#中使用HttpClient竟然遭遇超时,报错如下:

The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing

我们只要加长它的超时时间就可以了,我的使用场景是下载一个大文件时会遇到,

public class ReporterAccessLayer
{
   
    private readonly HttpClient _httpClient;
    private readonly IHttpClientFactory _httpFactory;
    private readonly JsonSerializerOptions _options;

    public ReporterAccessLayer(IHttpClientFactory httpFactory)
    {
        _httpFactory =httpFactory;
        _options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
    }

    public async Task<List<Reports>> GetAll()
    {
      try
      {
         HttpClient  httpClient=_httpFactory.CreateClient();
          httpClient.Timeout = TimeSpan.FromMinutes(10);
         return await httpClient.GetAsync<List<Reports>>("/ReportsPage/GatherAllReports");
      }
      catch
      {
         // do exception handling

      } 
    }

}

例如在这里我设计的超时时间为10分钟,下载一个200M的文件足够了。

公众号二维码

关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com

庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。

欧阳修

付款二维码

如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力