无法使用HttpClient发送任何请求

时间:2019-07-10 07:38:18

标签: c# .net-core httpclient

我正在尝试在HttpClient上使用IHttpClientFactory.net core 2.2发送请求,但收到如下的HttpRequestExceptionSocketException

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

我尝试增加客户端超时时间,并保持活动连接,但是没有运气。 另外,我发现this问题仍然存在,但是我不确定我的问题是否与此相同,因为我在此本地服务器上没有任何高负载。

    var request = new HttpRequestMessage(HttpMethod.Get, "https://jsonplaceholder.typicode.com/todos/1");

    var client = _clientFactory.CreateClient("client");
    client.Timeout = TimeSpan.FromSeconds(120);
    client.DefaultRequestHeaders.ConnectionClose = false;

    var response = client.SendAsync(request).Result;

在我的Startup.cs中,我有:

services.AddHttpClient("client");

0 个答案:

没有答案