带有 .Net Core 2.1 的 HttpClient 挂起 [英] HttpClient with .Net Core 2.1 hangs
问题描述
鉴于以下 .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 并且它工作正常...
注意
我试过使用:
HttpClientFactory -> Same result
WebRequest -> Same result
想法?
更新 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.
更新 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/30166https://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屋!