C# 调用腾讯云的短信发送服务API

首先需要在腾讯云平台上登录并实名信息认证。

记住自己的SDK AppID和App Key    

申请自己的短信签名和创建自己的短信模版。这两项需要腾讯云官方的审核,建议加客服的qq询问。这样会审核快一点。

然后就可以调用服务了。代码如下:

参数实体类:SendCode

namespace ConsoleApp3
{
    /// 
    /// 参数类
    /// 
    class SendCode
    {
        public string Ext { get; set; }
        public string Extend { get; set; }
        public string[] Params { get; set; }
        public string Sig { get; set; }
        public string Sign { get; set; }
        public Phone Tel { get; set; }
        public string Time { get; set; }
        public string Tpl_id { get; set; }
    }
    /// 
    /// 电话参数
    /// 
    class Phone
    {
        public string Mobile { get; set; }
        public string Nationcode { get; set; }
    }
}

 静态方法类:StaticClass

using System;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;

namespace ConsoleApp3
{
    public static class StaticClass
    {
        /// 
        /// 返回指定个数的随机数
        /// 
        /// 个数
        /// 随机数
        public static string GenerateRandomCode(int length)
        {
            var result = new StringBuilder();
            for (var i = 0; i < length; i++)
            {
                var r = new Random(Guid.NewGuid().GetHashCode());
                result.Append(r.Next(0, 10));
            }
            return result.ToString();
        }
        /// 
        /// 获取时间戳格式
        /// 
        /// 多少位的时间戳
        /// 时间戳
        public static long GetTimeStamp(int flg)
        {
            long time = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
            switch (flg)
            {
                case 10:
                    time = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
                    break;
                case 13:
                    DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
                    time = (DateTime.Now.Ticks - startTime.Ticks) / 10000;
                    break;
            }
            return time;
        }
        /// 
        /// Sha256加密算法
        /// 
        /// 加密的内容
        /// 加密后的数据
        public static string Sha256(string data)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(data);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < SHA256.Create().ComputeHash(bytes).Length; i++)
            {
                builder.Append(SHA256.Create().ComputeHash(bytes)[i].ToString("X2"));
            }
            return builder.ToString();
        }
        /// 
        /// post请求
        /// 
        /// 地址
        /// 参数
        /// 返回内容
        public static string HttpPost(string url, string postdata)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Referer = null;
            req.AllowAutoRedirect = true;
            req.Accept = "*/*";

            byte[] data = Encoding.UTF8.GetBytes(postdata);
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
            }
            try
            {
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
                {
                    result = reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
    }
}

 程序主入口:

using Newtonsoft.Json;
using System;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string mobile = "***********";//自己要验证收短信的手机号
            string appkey = "*****************";//自己在腾讯云上申请的App Key
            string random = StaticClass.GenerateRandomCode(10);
            string time = StaticClass.GetTimeStamp(10).ToString();
            string sig = StaticClass.Sha256($"appkey={appkey}&random={random}&time={time}&mobile={mobile}");
            var postData = new SendCode
            {
                Ext = "",
                Extend = "",
                Params = new string[] { "5566" },
                Sig = sig,
                Sign = "***********",//自己审核通过的短信签名
                Tel = new Phone { Mobile = "**********",/*自己要验证收短信的手机号*/ Nationcode = "86"/*国家标识*/ },
                Time = time,
                Tpl_id = "************"//自己审核通过的短信模版id
            };
            string url = $"https://yun.tim.qq.com/v5/tlssmssvr/sendsms?sdkappid=******&random={random}";//sdkappid=自己在腾讯云上申请的SDK AppID
            string postDataStr = JsonConvert.SerializeObject(postData).ToLower();
            string result = StaticClass.HttpPost(url, postDataStr);
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}

个人免费使用,只有100条短信的数量。但是够自己测试使用了。用自己的手机号测试,可以正常收到短信。

你可能感兴趣的:(C#,方法,腾讯云API)