Asp.net HttpClient.GetAsync可与LocalHost API一起使用,但不能与Live API一起使用

Asp.net HttpClient.GetAsync可与LocalHost API一起使用,但不能与Live API一起使用,asp.net,.net,ssl-certificate,Asp.net,.net,Ssl Certificate,我有一个.NET4.5.2WebApp正在调用我的API。当我将我的web应用程序指向本地主机版本的API时,它会获取数据,并且返回的很好。我发布了该API,并确认该API与PostMan正常工作。 然后,我运行完全相同的WebApp代码,仅将URI从localhost更改为live api,并得到一个由以下内容组成的多个异常错误: System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | Sec

我有一个.NET4.5.2WebApp正在调用我的API。当我将我的web应用程序指向本地主机版本的API时,它会获取数据,并且返回的很好。我发布了该API,并确认该API与PostMan正常工作。 然后,我运行完全相同的WebApp代码,仅将URI从localhost更改为live api,并得到一个由以下内容组成的多个异常错误:

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  • 远程主机已强制关闭现有连接
  • 无法从传输连接读取数据:远程主机强制关闭了现有连接
  • 基础连接已关闭:发送时发生意外错误
  • 发送请求时出错
  • 这是我的电话号码

     using (HttpClient client = new HttpClient())
     {
         client.DefaultRequestHeaders.Accept.Clear();
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
         client.DefaultRequestHeaders.Add("user", serializedUser);
         response = null;
         try
         {
               //Uri uri = new Uri("https://jsonplaceholder.typicode.com/posts/1");//https works
               Uri uri = new Uri("https://api.acme.com/values/test");
               //Uri uri = new Uri("http://localhost/5000/values/test"); //http localhost works
               response = client.GetAsync(uri).Result; 
          }
          catch (Exception e)
          {
              string er = e.Message;
          }
     }  
    
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    
    编辑1:我从头创建了一个.NET核心应用程序,我的原始代码可以完美地调用我的live API。我的原始代码也可以在.NET4.5.2中调用不同的“https”API

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    
    编辑2: 这就是我现在所处的位置,我从VS2015创建了两个通用应用程序,一个是.NET核心Web应用程序,另一个是.NET Framework Web应用程序。我在两个应用程序中使用了完全相同的上述代码来调用API。在这两个应用程序中,我都可以调用我在网上找到的通用“https”api(jsonplaceholder)。我也可以从这两个站点调用我的应用程序的本地主机版本“http”。在.NET核心版本的应用程序中,我可以调用我的“https”实时API,并获得我想要的结果。在.NET Framework应用程序中,我仍然会遇到相同的错误

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    

    我不知道我的核心请求和框架请求之间的区别是什么,它们一个被关闭,另一个没有被关闭。

    您似乎正在安全的http环境(https)上托管应用程序。您是否在承载Web API的服务器上使用SSL证书?如果不是,它可能会抛出与证书相关的异常

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    
    只需在调用GetAsync之前添加以下行,这将忽略SSL错误

    ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
    
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    
    这仅建议在无法伪造服务器身份的内部网环境或其他封闭网络中使用

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    

    在我的API调用之前添加以下行修复了这个问题,但我很想听听关于这一行的作用以及在我的web应用程序中使用它可能带来的任何安全风险的解释

    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
    

    这是道具

    不是我的领域,但我怀疑这与您从http更改为https有关。看看这篇文章是否有用:。你不应该发布代码的截图,你应该把代码放在你的问题中,然后使用网站编辑器中的
    {}
    对其进行格式化。这允许更好地搜索未来的访问者。@ScottChamberlain修复了它。这个修复对我不起作用,我尝试了你文章中的那个,以及你添加的链接中的servicepointmanager解决方案。两者都没有任何区别。
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;