.net core 3.1 服务器端获取用户信息经常出现
System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception.
---> System.IO.IOException: Authentication failed because the remote party has closed the transport stream.
at System.Net.Security.SslStream.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
at System.Net.Security.SslStream.PartialFrameCallback(AsyncProtocolRequest asyncRequest)
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Security.SslStream.EndProcessAuthentication(IAsyncResult result)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.EstablishSslConnectionAsyncCore(Stream stream, SslClientAuthenticationOptions sslOptions, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
不是必然,是经常出现 ,请问是什么问题啊
请求代码
var hander = new HttpClientHandler();
hander.ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true;
HttpClient webClient = new HttpClient(hander);
var jsonString =
await (await webClient.GetAsync(
$"https://api.weixin.qq.com/sns/jscode2session?appid={SystemSetting.WeClientMiniAppID}&secret={SystemSetting.WeClientMiniAppSecret}&js_code={request.WeCode}&grant_type=authorization_code"
)).Content.ReadAsStringAsync();
我也遇到了相同的问题,core2.0升级到3.1后,这个错误出现的概率大约1%,我换一台服务器测试就没问题,所以应该不是代码的原因,应该是服务器配置的问题。目前猜测,应该是服务器安装的ASP.NET Core Runtime版本不对,需要升级(我暂时还没验证)。
https://dotnet.microsoft.com/en-us/download/dotnet/3.1
我想到个办法,失败的话再次调用接口,就可以了。
异常信息:
The SSL connection could not be established, see inner exception.InnerExceptionMessage:Authentication failed because the remote party has closed the transport stream.
我这跟你一样的问题,大佬解决了没有啊,跪求!!!
后端接口详见https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html
前端只要保证传给你的code是新的,且没有任何接口使用过就行。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;