首页 新闻 搜索 专区 学院

.net core 3.1 HttpClient The operation was canceled

0
悬赏园豆:5 [已解决问题] 解决于 2021-01-04 09:30

HttpResponseMessage httpResponseMessage = await _httpClient.SendAsync(new HttpRequestMessage()
{
Method = HttpMethod.Post,
Content = new StringContent(body, Encoding.UTF8, "application/json"),
RequestUri = new Uri(url)
}, HttpCompletionOption.ResponseHeadersRead);
using (httpResponseMessage)
{
httpResponseMessage.EnsureSuccessStatusCode();
content = await httpResponseMessage.Content.ReadAsStringAsync();
}
.net core 3.1在部署在linux中,会偶尔出现如下错误,有大神遇到过吗?

---> System.Threading.Tasks.TaskCanceledException: The operation was canceled.
---> System.IO.IOException: Unable to read data from the transport connection: Operation canceled.
---> System.Net.Sockets.SocketException (125): Operation canceled
--- End of inner exception stack trace ---
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.GetResult(Int16 token)
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at Microsoft.Extensions.Http.Logging.LoggingScopeHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)

CHild.的主页 CHild. | 初学一级 | 园豆:4
提问于:2020-12-29 10:54
< >
分享
最佳答案
0

错误意思是读取不到数据.

  1. 检查请求的server是不是请求挂起了,或者耗时太严重
  2. 检查网络是不是有问题.
  3. client程序cpu是否过高, threadpool是否足够.
收获园豆:4
czd890 | 专家六级 |园豆:12614 | 2020-12-29 13:14
其他回答(1)
0

HttpClient 请求超时也会出现这个错误

收获园豆:1
dudu | 园豆:36417 (高人七级) | 2020-12-29 13:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册