首页 > Web开发 > 详细

.net调用阿里短信接口

时间:2019-06-05 11:59:16      阅读:124      评论:0      收藏:0      [点我收藏+]

一.创建一个空的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

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

 

 

 

.net调用阿里短信接口

原文:https://www.cnblogs.com/liguix/p/10978406.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!