ITPub博客

首页 > IT基础架构 > 网络通信/物联网 > 怎么利用C#发送验证码短信

怎么利用C#发送验证码短信

原创 网络通信/物联网 作者:乐讯通云通信 时间:2024-10-10 14:50:43 0 删除 编辑

在数字化高速发展的今天,无论是日常购物、银行交易、账号注册还是登录验证,短信验证码已成为我们生活中不可或缺的一部分。它以其便捷性、即时性和相对较高的隐私性,成为了数字时代保障用户信息隐私的重要工具。


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinApiDemo
{
    class Program
    {
        private const string url = "http://www.lokapi.cn/smsUTF8.aspx";
        private const string urlReply = "http://www.lokapi.cn/callApi.aspx";
        private const string urlStatus = "http://www.lokapi.cn/statusApi.aspx";
        private const string urlBalance = "http://www.lokapi.cn/smsUTF8.aspx";
        private const string urlKeyword = "http://www.lokapi.cn/checkWord.aspx";
        private const string rece = "json";
        private const string username = "";
        private const string password = "";
        private const string encode = "utf-8";
        //token
        private const string tokenYZM = "";//验证码
        //模板ID
        private const string templateid = "";
        //参数
        private const string param = "17733861234|2541";
        private const string mobile = "";
        private const string title = "祝福短信";//彩信标题
        static void Main(string[] args)
        {
            string result = "";
            string sign = "";
            string passwordMd5 = Common.Md5Hash(password);
            string ticks = ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000).ToString();
            Encoding encoding = Encoding.GetEncoding(encode);
            StringBuilder sb = new StringBuilder();
            #region 文字短信
            sb.AppendFormat("action=sendtemplate&username={0}&password={1}&token={2}&timestamp={3}", username, passwordMd5, tokenYZM, ticks);
            sign = Common.Md5Hash(sb.ToString());
            sb.AppendFormat("&sign={0}&rece={1}", sign, rece);
            sb.AppendFormat("&templateid={0}&param={1}", templateid, param);
            result = Common.HttpPost(url, sb.ToString(), encoding);
            Console.WriteLine(result);
            #endregion
 #region 文字回复报告
            sb.Clear();
            sb.AppendFormat("action=sms&username={0}&password={1}&timestamp={2}", username, passwordMd5, ticks);
            sign = Common.Md5Hash(sb.ToString());
            sb.AppendFormat("&sign={0}&rece={1}", sign, rece);
            result = Common.HttpPost(urlReply, sb.ToString(), encoding);
            Console.WriteLine(result);
            #endregion
            #region 状态报告
            sb.Clear();
            //action根据API文档选择合适产品的action
            sb.AppendFormat("action=sms&username={0}&password={1}&timestamp={2}", username, passwordMd5, ticks);
            sign = Common.Md5Hash(sb.ToString());
            sb.AppendFormat("&sign={0}&rece={1}", sign, rece);
            result = Common.HttpPost(urlStatus, sb.ToString(), encoding);
            Console.WriteLine(result);
            #endregion
            #region 余额查询
            sb.Clear();
            sb.AppendFormat("action=overage&username={0}&password={1}&token={2}&timestamp={3}", username, passwordMd5, tokenTZ, ticks);
            sign = Common.Md5Hash(sb.ToString());
            sb.AppendFormat("&sign={0}&rece={1}", sign, rece);
            result = Common.HttpPost(urlBalance, sb.ToString(), encoding);
            Console.WriteLine(result);
            #endregion
            #region 屏蔽词检测
            sb.Clear();
            sb.AppendFormat("username={0}&password={1}&timestamp={2}", username, passwordMd5, ticks);
            sign = Common.Md5Hash(sb.ToString());
            sb.AppendFormat("&sign={0}&rece={1}&message={2}", sign, rece, "您的验证码是5412");
            result = Common.HttpPost(urlKeyword, sb.ToString(), encoding);
            Console.WriteLine(result);
            #endregion
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace WinApiDemo
{
    public class Common
    {
        public static string HttpPost(string Url, string Body, Encoding encode)
        {
            string ResponseContent = "";
            try
            {
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
                httpWebRequest.ContentType = "application/x-www-form-urlencoded";
                httpWebRequest.Method = "POST";
                httpWebRequest.Timeout = 600000; //setInstanceFollowRedirects
                byte[] btBodys = encode.GetBytes(Body);
                httpWebRequest.ContentLength = btBodys.Length;
                Stream reqStream = httpWebRequest.GetRequestStream();
                reqStream.Write(btBodys, 0, btBodys.Length);
                HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream resStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(resStream, encode);
                ResponseContent = streamReader.ReadToEnd();
                streamReader.Close();
                resStream.Close();
                reqStream.Close();
                streamReader.Dispose();
                resStream.Dispose();
                reqStream.Dispose();
                httpWebResponse.Close();
                httpWebResponse.Dispose();
                httpWebRequest.Abort();
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
            return ResponseContent;
        }
        public static string Md5Hash(string input)
        {
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString().ToUpper();
        }
    }
}


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70036589/viewspace-3035790/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2024-01-16

  • 博文量
    4
  • 访问量
    2911