简体   繁体   English

带有.Net Core 2.1的HttpClient挂起

[英]HttpClient with .Net Core 2.1 hangs

Given the following .Net Core 2.1 Console App... 鉴于以下.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());
            }
        }
    }
}

The call to GetAsync hangs throwing an exception with the following message: 对GetAsync的调用挂起抛出异常,并显示以下消息:

System.Net.Http.HttpRequestException: 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 ---> System.Net.Sockets.SocketException: 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 System.Net.Http.HttpRequestException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应---> System.Net.Sockets.SocketException:A连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应

However, switch to .Net Core 2.0 and it works fine... 但是,切换到.Net Core 2.0,它工作正常......

NOTE 注意

I've tried using: 我尝试过使用:

HttpClientFactory -> Same result
WebRequest        -> Same result

Thoughts? 思考?

UPDATE 1 This works when not on the corporate network which might mean a change in behavior with the proxy perhaps. 更新1当不在公司网络上时,这可能意味着可能意味着代理行为的改变。 However, core2.0 still works regardless so trying to find the difference. 然而,core2.0仍然可以工作,所以试图找到差异。

UPDATE 2 Looks like a bug was introduced and it is reported... 更新2看起来像一个错误被引入并报告...

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

The was a change in CoreFx 2.1 that causes the HttpClient to use a new HttpClientHandler . 这是CoreFx 2.1的一个变化,导致HttpClient使用新的HttpClientHandler This is possibly the cause of your problem and why downgrading works. 这可能是您的问题的原因以及降级的原因。

There are many ways to reset the handler and you can read more about it in the change log . 有许多方法可以重置处理程序,您可以在更改日志中阅读有关它的更多信息。 You can use the old HttpHandler by instantiating an HttpClient with a WinHttpHandler as the parameter, setting the environment variable DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER to false, or by calling the following in your code: 您可以通过使用WinHttpHandler作为参数实例化HttpClient,将环境变量DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER设置为false,或者在代码中调用以下内容来使用旧的HttpHandler:

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

So apparently there is a bug/breaking change reported on this. 显然,有一个错误/突破性变化报告。

Here: https://github.com/dotnet/corefx/issues/30166 https://github.com/dotnet/corefx/issues/30191 这里: https//github.com/dotnet/corefx/issues/30166 https://github.com/dotnet/corefx/issues/30191

Two separate but related issues that I believe is what I am experiencing. 我认为是我遇到的两个独立但相关的问题。

However, I found what appears to be a workaround. 但是,我发现似乎是一种解决方法。

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;
            }
        }
    }
}

The key part here is to use WinHttpHandler and set the WindowsProxyUsePolicy to WindowsProxyUsePolicy.UseWinInetProxy 这里的关键部分是使用WinHttpHandler并将WindowsProxyUsePolicy设置为WindowsProxyUsePolicy.UseWinInetProxy

WinHttpHandler is found by adding nuget package System.Net.Http.WinHttpHandler WinHttpHandler通过添加NuGet包发现System.Net.Http.WinHttpHandler

这是一个着名的“21秒”超时问题...在Azure中提供罕见呼叫服务(我的服务是从Azure基础架构调用外部服务)有助于添加:

httpClient.DefaultRequestHeaders.ConnectionClose = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM