c# - HttpClient 卡在异步任务上
问题描述
我有一个 HttpClient,我正在使用以下代码根据远程服务器上的文件提取流。当线程到达第三个等待时,它会在那里暂停很长时间。如果文件的大小妨碍它们分别为 33347 kb、123665 kb 和 178688 kb,但我怀疑是这种情况,因为最后两个大小相似。
// a property in a class
private HttpClient HttpClient { get; set; } = new HttpClient() { Timeout = TimeSpan.FromMilliseconds(-1) };
// in a function in that class
using Stream stationsStream = await HttpClient.GetStreamAsync("https://eddb.io/archive/v6/stations.jsonl");
using Stream systemsStream = await HttpClient.GetStreamAsync("https://eddb.io/archive/v6/systems_populated.jsonl");
// always gets stuck on the third await no matter the order.
using Stream listingStream = await HttpClient.GetStreamAsync("https://eddb.io/archive/v6/listings.csv");
我没有过多地使用 HttpClient 类,但提出的一些理论是 API 只允许两个连接,或者 HttpClient 只能接受两个。
解决方案
我需要HttpClient
用 a创建并将 'HttpClientHandler
传递给HttpClientHandler
'HttpClient
的构造函数,同时将HttpClientHandler
'更改MaxConnectionsPerServer
为更大的数字。例如:
private HttpClient HttpClient { get; set; } = new HttpClient(Handler) { Timeout = TimeSpan.FromMilliseconds(-1) };
private static HttpClientHandler Handler { get; set; } = new HttpClientHandler() { MaxConnectionsPerServer = 3 };
推荐阅读
- r - Summarise_at 其中 1 行仅包含 R 中的 NA
- javascript - 如何在 .vue 文件中导入类(.js 文件)?
- python - 使用服务主体连接到 ADLS Gen 2
- react-native - 如何为 React Native 应用程序的每个用户获取唯一 ID
- amazon-web-services - TextRact 响应状态码不表示成功:400(错误请求)+ IAM Keys not found
- cmake - 如何在使用 ExternalProject_Add 添加的外部项目上设置 CMake 策略和属性
- javascript - 在javascript中有条件地返回一个对象属性
- python - load_model("test.mod",True/False,False) 给我一个 ZeroDivisionError: 除以零
- sql - 通过 SSIS 控制文件
- math - 如何标准化两个极限之间的角度?