最近.net core 项目中遇到一个问题,通过Httpclient 访问https的接口报错,错误如下:

WinHttpException: A security error occurred
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult()
System.Net.Http.WinHttpHandler+<StartRequest>d__105.MoveNext() HttpRequestException: An error occurred while sending the request.
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.ConfiguredTaskAwaitable+ConfiguredTaskAwaiter.GetResult()
System.Net.Http.HttpClient+<FinishSendAsync>d__58.MoveNext()
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
MyApp.Web.Controllers.HomeController.Test() in HomeController.cs
var response = client.GetAsync("https://someurl.com/api.php?arg1=some&arg2=test").GetAwaiter().GetResult();
lambda_method(Closure , object , Object[] )
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker+<InvokeActionMethodAsync>d__27.MoveNext()

通过排查发现是.NETFramework 4.6包括一个新的安全特性,它阻止连接的不安全密码和散列算法。 可以通过下面的解决方案处理:
using (var handler = new HttpClientHandler())
{
handler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
handler.SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls;
using (HttpClient client = new HttpClient(handler))
{
string requestObjJson = requestObj.ToJson();
var address = new Uri($"https://yourcompany.com/");
string token = GetToken();
client.BaseAddress = address;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
var contentData = new StringContent(requestObjJson, System.Text.Encoding.UTF8, "application/json");
using (var response = await client.PostAsync("yourcompany/api", contentData))
{
var content = response.Content.ReadAsStringAsync();
var taskResult = content.Result;
JObject resultObj = JObject.Parse(taskResult);
return resultObj;
}
}
}

需要注意的是,对于.NET core 2.0,您需要使用HttpClientHandler而不是ServicePointManager.

.net core 2.0 HTTPS request fails using HttpClient 安全错误的更多相关文章

  1. 用VSCode开发一个asp.net core 2.0+angular 5项目(4): Angular5全局错误处理

    第一部分: http://www.cnblogs.com/cgzl/p/8478993.html 第二部分: http://www.cnblogs.com/cgzl/p/8481825.html 第三 ...

  2. ASP.NET Core 1.0 基础与应用启动

    .NET Core http://dotnet.github.io/[https://github.com/dotnet/coreclr] ASP.NET Core 1.0 https://get.a ...

  3. ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1)

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  4. ASP.NET Core 1.0 部署 HTTPS

    ASP.NET Core 1.0 部署 HTTPS ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1) 提示 更新时间:2016年01月23日. 在目前介 ...

  5. .net core 3.0一个记录request和respose的中间件

    参考资料 https://www.cnblogs.com/wybin6412/p/10944077.html RequestResponseLog.cs using System; using Sys ...

  6. 使用Enablebuffering多次读取Asp Net Core 3.0 请求体 读取Request.Body流

    原文:使用Enablebuffering多次读取Asp Net Core 请求体 使用Enablebuffering多次读取Asp Net Core 请求体 1 .Net Core 2.X时代 使用E ...

  7. ASP.NET Core 2.0 中读取 Request.Body 的正确姿势

    原文:ASP.NET Core 中读取 Request.Body 的正确姿势 ASP.NET Core 中的 Request.Body 虽然是一个 Stream ,但它是一个与众不同的 Stream ...

  8. centos 7 + Net Core 3.0 + Docker 配置说明(不含https)

    1.新建Core3.0项目 1.1 使用visual studio 2019 创建一个名为core3.web.httpapi 的"ASP.NET Core Web应用程序" 1.2 ...

  9. ASP.NET Core 5.0 中读取Request中Body信息

    ASP.NET Core 5.0 中读取Request中Body信息 记录一下如何读取Request中Body信息 public class ValuesController : Controller ...

随机推荐

  1. HBASE 基础命令总结

    HBASE基础命令总结 一,概述 本文中介绍了hbase的基础命令,作者既有记录总结hbase基础命令的目的还有本着分享的精神,和广大读者一起进步.本文的hbase版本是:HBase 1.2.0-cd ...

  2. saltstack总结-2018-0620

    以下结论 结论1由于minion配置文件里能配置的只有master的IP和master的ret_port,而无法指定master的publish_port因此minion获取的master的publi ...

  3. neo4j通过LOAD CSV导入结点和关系

    1.neo4j默认的导入入口是:安装路径/import,所以要将csv文件放在import目录下,像下面这样: 2.导入后中文乱码: 因为neo4j是utf-8的,而CSV默认保存是ANSI的,需要用 ...

  4. windows 端口转发

    1.添加端口转发 netsh interface portproxy add v4tov4 listenport=5000 listenaddress=10.30.3.148 connectport= ...

  5. Sql Server查看死锁及堵塞脚本

    --每秒死锁数量 SELECT * FROM sys.dm_os_performance_counters WHERE counter_name LIKE 'Number of Deadlocksc% ...

  6. webAPP如何实现移动端拍照上传(Vue组件示例)?

    摘要:使用HTML5编写移动Web应用,主要是为了尝试一下“一套代码多处运行”,一个webapp几乎可以不加修改的运行在PC/Android/iOS等上面运行.但是写到现在觉得虽然这种方式弊大于利,不 ...

  7. [转][C#]ImageHelper

    { internal static class ImageHelper { public static Bitmap CloneBitmap(Image source) { if (source == ...

  8. AX2012 ERP 维度相关表数据关系图

    AX2012比AX2009可怜的几个维度来说,太丰富了,可以无数个啊.维度多了,如何使用以及管理是个问题.这个需要在做调研时,充分分析讨论确定.以下为维度表关联关系,在做SQL取值时需要了解下,比如在 ...

  9. 一则ORACLE进程都在但是无法进入实例的问题

    [oracle@localhost ~]$ ps -ef|grep smonoracle 14809 1 0 Sep25 ? 00:13:02 ora_smon_mailp3[oracle@local ...

  10. 常用font-family

    微软雅黑: Microsoft YaHei 宋体:SimSun 黑体:SimHei 仿宋: FangSong 楷体:  KaiTi 隶书:LiSu 幼圆:YouYuan 华文细黑:STXihei 华文 ...