HttpClient client.GetAsync 适用于完整的 .Net 框架,但不适用于 .Net core 2.1? [英] HttpClient client.GetAsync works in full .Net framework but not in .Net core 2.1?
问题描述
我有以下代码(需要将 C# 更改为 7.1+ 版)在我的公司网络中运行.它适用于完整的 .Net 框架,但不适用于 .Net core 2.1 应用程序?为什么?(查看https站点的证书会显示部分证书是我公司颁发的)
I have the following code (need to change C# to version 7.1+) running in my company network. It works in full .Net framework but not in .Net core 2.1 application? Why? (Checking the Certificate of the https sites will show some of the certificates are issued by my company)
public static async Task Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
var client = new HttpClient();
var response = await client.GetAsync("https://usbtrustgateway.usbank.com/portal/");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
Console.WriteLine(content);
}
它在 .Net core 中的 client.GetAsync(...)
行上得到以下异常
It gets the following exception on the line client.GetAsync(...)
in .Net core
抛出异常:System.Private.CoreLib.dll 中的System.Net.Http.HttpRequestException"(无法建立 SSL 连接,请参阅内部异常.")抛出异常:System.Net.Http.HttpRequestException"' 在 System.Private.CoreLib.dll 中(无法建立 SSL 连接,请参阅内部异常.")超链接:激活历史调试 0.95 秒 [15948] 工作线程
Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll ("The SSL connection could not be established, see inner exception.") Exception thrown: 'System.Net.Http.HttpRequestException' in System.Private.CoreLib.dll ("The SSL connection could not be established, see inner exception.") Hyperlink: Activate Historical Debugging 0.95s [15948] Worker Thread
内部异常是
现有连接被远程主机强行关闭
An existing connection was forcibly closed by the remote host
推荐答案
我关注这个帖子有一段时间了,我很好奇下面的帖子是否会修复它.我们在 .Net Core 上遇到了类似的问题.
I've been following this thread for a while, and I'm curious if the follow will fix it. We ran into a similar issue with .Net Core.
在您的 appsettings.json 中,尝试添加...
In your appsettings.json, try adding...
"DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER": "0"
"DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER": "0"
示例:
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "https://localhost:44349/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_SYSTEM_NET_HTTP_USESOCKETSHTTPHANDLER": "0"
}
},
这篇关于HttpClient client.GetAsync 适用于完整的 .Net 框架,但不适用于 .Net core 2.1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!