C#HttpClient关闭ssl(https)方法,暨System.Net.Http.WinHttpException: 发生了安全错误 解决方案

本文介绍如何使用C#中的HttpClient处理服务器证书验证,通过自定义ServerCertificateCustomValidationCallback委托实现信任所有证书,适用于开发测试阶段。
            var handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = delegate { return true; };

            var client = new HttpClient(handler);

 

HttpResponseMessage respon = await hp.PostAsync(interfaceurl, new StringContent(JsonConvert.SerializeObject(dic, jsetting), Encoding.UTF8, "application/json"));             string msm = await respon.Content.ReadAsStringAsync();HttpResponseMessage respon = await hp.PostAsync(interfaceurl, new StringContent(JsonConvert.SerializeObject(dic, jsetting), Encoding.UTF8, "application/json"));报错{System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure: RemoteCertificateNameMismatch at System.Net.Security.SslStream.CompleteHandshake(SslAuthenticationOptions sslAuthenticationOptions) at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](Boolean receiveFirst, Byte[] reAuthenticationData, CancellationToken cancellationToken) at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken) --- End of inner exception stack trace --- at System.Net.Http.ConnectHelper.EstablishSslConnectionAsync(SslClientAuthenticationOptions sslOptions, HttpRequestMessage request, Boolean async, Stream stream, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem) at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken) at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken) at System.Net.Http.DiagnosticsHandler.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken) at System.Net.Http.HttpClient.g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken) at MyApi.BigMachine.BigMachineInterface.InvokeInterface(String name, Dictionary`2 data, String YH_BigMachinedmpath) in D:\hhh\VSCode项目\后端API\KHDAPIXM\MyApi\BigMachine\BigMachineInterface.cs:line 44 at MyApi.BigMachine.BigMachineInterface.IsLiangQingLocked(List`1 _XYID, String YH_BigMachinedmpath) in D:\hhh\VSCode项目\后端API\KHDAPIXM\MyApi\BigMachine\BigMachineInterface.cs:line 2430 at MyApi.Controllers.XieYiController.BindXieYi(Nullable`1 pageSizeValue, Nullable`1 pageNumberValue, String HeTongBianHao, String ModuleID, String JDID, String YH_BigMachinedmpath) in D:\hhh\VSCode项目\后端API\KHDAPIXM\MyApi\Controllers\XieYiController.cs:line 229}怎么解决在vscode的api中
最新发布
08-08
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上海好程序员

给上海好程序员加个鸡腿!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值