带有.Net Core 2.1的HttpClient挂起 [英] HttpClient with .Net Core 2.1 hangs

查看:173
本文介绍了带有.Net Core 2.1的HttpClient挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Given the following .Net Core 2.1 Console App...

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的调用挂起并引发异常,并显示以下消息:

The call to GetAsync hangs throwing an exception with the following message:


System.Net.Http.HttpRequestException:连接尝试失败
因为连接的一方在
的时间后未正确响应,或者由于连接的主机具有
的响应失败,所以建立的连接失败---> System.Net.Sockets.SocketException:A
连接尝试失败,因为连接的一方在一段时间后未正确响应
,或者建立的连接失败
因为连接的主机未能响应

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

但是,切换到.Net Core 2.0,它可以正常工作...

注意

NOTE

我尝试使用:

HttpClientFactory -> Same result
WebRequest        -> Same result

有什么想法吗?

UPDATE 1
此功能在不在公司网络上时有效,这可能意味着代理的行为发生了变化。但是,无论如何尝试找到区别,core2.0仍然有效。

UPDATE 1 This works when not on the corporate network which might mean a change in behavior with the proxy perhaps. However, core2.0 still works regardless so trying to find the difference.

UPDATE 2
看起来像是引入了一个错误,并且据报道...

UPDATE 2 Looks like a bug was introduced and it is reported...

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

推荐答案

显然,这是一个错误/重大变化的报告。

So apparently there is a bug/breaking change reported on this.

此处:
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;
            }
        }
    }
}

这里的关键部分是使用 WinHttpHandler 并将 WindowsProxyUsePolicy 设置为 WindowsProxyUsePolicy.UseWinInetProxy

The key part here is to use WinHttpHandler and set the WindowsProxyUsePolicy to WindowsProxyUsePolicy.UseWinInetProxy

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

WinHttpHandler is found by adding nuget package System.Net.Http.WinHttpHandler

这篇关于带有.Net Core 2.1的HttpClient挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆