开发者社区 问答 正文

使用阿里云sdk发短信提示SSL connection could not be established

问题描述

参考https://next.api.aliyun.com/api-tools/sdk/Dysmsapi?version=2017-05-25&language=csharp-tea这里的C#demo在发短信时报以下错误

  One or more errors occurred. (The SSL connection could not be established, see inner exception.)

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.

代码如下:

SmsClient = CreateClient(AccessKeyId, AccessKeySecret);

var msg = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest()

                {

                    PhoneNumbers = quest.to,

                    SignName = SignName,

                    TemplateCode = TemplateCode,

                    TemplateParam = string.Format("{{\"msg\":\"{0}\"}}", quest.text),

                };

                ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;  ///参考网上方法加了这个也不行

                

                AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsResponse res = SmsClient.SendSms(msg);  //这里报错 

....

public static Client CreateClient(string accessKeyId, string accessKeySecret)

        {

            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config

            {

                // 您的 AccessKey ID

                AccessKeyId = accessKeyId,

                // 您的 AccessKey Secret

                AccessKeySecret = accessKeySecret,

            };

            // 访问的域名

            config.Endpoint = "dysmsapi.aliyuncs.com";

            return new Client(config);

        }

完整异常信息如下:

2022-09-23 16:49:14,541 - Aliyun_smsService SendSMS Error:One or more errors occurred. (The SSL connection could not be established, see inner exception.)

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.

   at System.Net.Security.SslStream.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, ExceptionDispatchInfo exception)

   at System.Net.Security.SslStream.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)

...

   at AlibabaCloud.OpenApiClient.Client.DoRequest(Params params_, OpenApiRequest request, RuntimeOptions runtime)

   at AlibabaCloud.OpenApiClient.Client.CallApi(Params params_, OpenApiRequest request, RuntimeOptions runtime)

   at AlibabaCloud.SDK.Dysmsapi20170525.Client.SendSmsWithOptions(SendSmsRequest request, RuntimeOptions runtime)

   at AlibabaCloud.SDK.Dysmsapi20170525.Client.SendSms(SendSmsRequest request)

期望结果

解决发短信问题

已尝试的方法

下面两个在SendSms试了都不行。

ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;

ServicePointManager.ServerCertificateValidationCallback =
        delegate (
            object s,
            X509Certificate certificate,
            X509Chain chain,
            SslPolicyErrors sslPolicyErrors
        ) {
            return true;
        };

展开
收起
游客4dv2kctwk475s 2022-09-23 17:25:13 553 分享 版权
1 条回答
写回答
取消 提交回答