.net short message interface to call Ali

A. Create an empty project api

II. Ali SMS application package aliyun-net-sdk-core

III. Log in and add a signature template Ali

IV. Create Create AccessKey

Note AccessKey created, can no longer be viewed through the console. To download has been saved.

V. generate interface code

Fill in the relevant information directly generated code

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

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

 

 

 

Guess you like

Origin www.cnblogs.com/liguix/p/10978406.html