HttpClient client.GetAsync可在完整的.Net框架中工作,但不能在.Net核心2.1中工作? [英] HttpClient client.GetAsync works in full .Net framework but not in .Net core 2.1?

查看:250
本文介绍了HttpClient client.GetAsync可在完整的.Net框架中工作,但不能在.Net核心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核心中的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 'in System.Private.CoreLib.dll中(无法建立SSL连接,请参阅内部异常.")超链接:激活历史调试0.95s [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核心2.1中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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