zoukankan      html  css  js  c++  java
  • .net调用阿里短信接口

    一.创建一个空的api项目

    二.应用阿里的短信包 aliyun-net-sdk-core

    三.登录阿里添加签名和模板

    四.创建创建AccessKey

    注意 AccessKey创建后,无法再通过控制台查看。一直要下载下来保存。

    五.生成接口代码

    填入相关信息直接生成代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Exceptions;
    using Aliyun.Acs.Core.Http;
    using Aliyun.Acs.Core.Profile;
    using Microsoft.AspNetCore.Mvc;
    
    namespace NoteDemo.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class ValuesController : ControllerBase
        {
            // GET api/values
            [HttpGet]
            public ActionResult<string> Get()
            {
                var msg = "";
                //注意刚刚下载的AccessKey的excel中的accessKeyId和accessSecret填入
                IClientProfile profile = DefaultProfile.GetProfile("default", "<accessKeyId>", "<accessSecret>");
                DefaultAcsClient client = new DefaultAcsClient(profile);
                CommonRequest request = new CommonRequest();
                request.Method = MethodType.POST;
                request.Domain = "dysmsapi.aliyuncs.com";
                request.Version = "2017-05-25";
                request.Action = "SendSms";
                // request.Protocol = ProtocolType.HTTP;
                request.AddQueryParameters("PhoneNumbers", "手机号");
                request.AddQueryParameters("SignName", "签名");
                request.AddQueryParameters("TemplateCode", "模板");
                // request.Protocol = ProtocolType.HTTP;
    
                try
                {
                    CommonResponse response = client.GetCommonResponse(request);
                    msg=System.Text.Encoding.Default.GetString(response.HttpResponse.Content);
                }
                catch (ServerException e)
                {
                    msg = e.ErrorMessage;
                }
                catch (ClientException e)
                {
                    msg = e.ErrorMessage;
                }
                return msg;
            }
        }
    }

    直接运行即可localhost:52374/api/Values

    前端调用,直接调用该接口地址即可。

     
  • 相关阅读:
    第一道题:无头苍蝇装头术(望不吝赐教)
    jdk8 list是否包含某值的一些应用
    Failed to close server connection after message failures; nested exception is javax.mail.MessagingException: Can't send command to SMTP host
    itext pdf加密
    TiDB-禁用遥测功能
    TiDB-配置调整
    DM-表空间
    DM-INI参数配置
    DM-DSC集群配置
    PG-并行查询
  • 原文地址:https://www.cnblogs.com/liguix/p/10978406.html
Copyright © 2011-2022 走看看