【问题标题】:HttpClient Get or Post doesn't work with .NETCore 2.1 and 2.2, but works with .NETCore 2.0HttpClient Get 或 Post 不适用于 .NETCore 2.1 和 2.2,但适用于 .NETCore 2.0
【发布时间】:2019-05-30 07:38:19
【问题描述】:

我使用 Visual Studio 2017 v15.9.4、.NET Standard 2.0.3 和最新版本的 .NETCore 2.1.6。我在 Windows 10 64 位上运行我的应用程序。

这段代码:

 using (HttpClient client = new HttpClient())
            {
                try
                {
                    var response = await client.GetAsync("http://httpbin.org/")
                ...

不适用于 .NET Core 2.1 或 2.2 的最新版本。 我有这个例外:

Result StackTrace:  
at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at EliorNugetCommon.Tests.APIManagerTests.Get() in D:\Git\Elior-Group\WebApiServices\NugetWAS\EliorNugetCommon\EliorNugetCommon.Tests\APIManagerTests.cs:line 50
----- Inner Stack Trace -----
Result Message: 
System.AggregateException : One or more errors occurred. (A task was canceled.)
---- System.Threading.Tasks.TaskCanceledException : A task was canceled.

但是,这适用于 .NET Core 2.0.9!

感谢您的帮助。

【问题讨论】:

  • 你在打多个电话吗?
  • 只是出于好奇。尝试使 Get 调用同步。 (仅限故障排除步骤)
  • 只有外部调用失败。如果 .NET Core 2.1 和 2.2 不使用代理,现在怎么办?

标签: visual-studio-2017 asp.net-core-2.1


【解决方案1】:

使用 .NET Core 和 .NET Core 2.2 中的新 HTTP 堆栈,代理背后的身份验证出现回归。在这种情况下,我们可以使用 .NET Core 2.0 HTTP stack 这个开关:

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

更多信息,讨论在这里:https://github.com/dotnet/corefx/issues/30166

【讨论】:

    猜你喜欢
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2017-01-25
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多