带有.Net Core 2.1的HttpClient挂起

时间:2018-06-07 19:40:10

标签: c# .net .net-core asp.net-core-2.1 .net-core-2.1

鉴于以下.Net Core 2.1控制台应用程序......

using System;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Headers;

namespace TestHttpClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));                    

                    string url = "https://jsonplaceholder.typicode.com/posts/1";
                    var response = httpClient.GetAsync(url).Result;
                    string jsonResult = response.Content.ReadAsStringAsync().Result;   
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
    }
}

对GetAsync的调用会抛出异常,并显示以下消息:

  

System.Net.Http.HttpRequestException:连接尝试失败   因为关联方在一段时间后没有正确回应   时间或已建立的连接因连接的主机而失败   未能回复---> System.Net.Sockets.SocketException:A   连接尝试失败,因为连接方没有正确   一段时间后回复,或建立连接失败   因为连接主机无法响应

但是,切换到.Net Core 2.0并且工作正常......

我尝试过使用:

HttpClientFactory -> Same result
WebRequest        -> Same result

思想?

更新1 当不在公司网络上时,这可能意味着可能意味着代理行为的改变。然而,core2.0仍然可以工作,所以试图找到差异。

更新2 看起来像是引入了一个bug并且报告了......

https://github.com/dotnet/corefx/issues/30166#issuecomment-395489603

3 个答案:

答案 0 :(得分:10)

CoreFx 2.1的变化导致HttpClient使用新的HttpClientHandler。这可能是您的问题的原因以及降级的原因。

有许多方法可以重置处理程序,您可以在change log中阅读更多相关信息。您可以通过使用WinHttpHandler作为参数实例化HttpClient,将环境变量DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER设置为false,或者在代码中调用以下内容来使用旧的HttpHandler:

AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);

答案 1 :(得分:8)

因此显然有一个错误/突破性变化。

下面: https://github.com/dotnet/corefx/issues/30166 https://github.com/dotnet/corefx/issues/30191

我认为是我遇到的两个独立但相关的问题。

但是,我发现似乎是一种解决方法。

Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), twoclick[i], options);
Bitmap scaledBm = Bitmap.createScaledBitmap(bitmap, WIDTH, HEIGHT, true);

此处的关键部分是使用using System; using System.Diagnostics; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; namespace TestHttpClient { static class Program { static async Task Main(string[] args) { try { using (var httpClient = new HttpClient(new WinHttpHandler() { WindowsProxyUsePolicy = WindowsProxyUsePolicy.UseWinInetProxy })) { httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); string url = "https://jsonplaceholder.typicode.com/posts/1"; var response = await httpClient.GetAsync(url); string jsonResult = await response.Content.ReadAsStringAsync(); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); throw; } } } } 并将WinHttpHandler设置为WindowsProxyUsePolicy

通过添加nuget package System.Net.Http.WinHttpHandler

找到

WindowsProxyUsePolicy.UseWinInetProxy

答案 2 :(得分:0)

这是一个著名的“ 21秒”超时问题... 在Azure中通过很少的调用(我的服务从Azure基础结构调用外部服务)来帮助服务的原因是:

httpClient.DefaultRequestHeaders.ConnectionClose = true;