MAS短信平台发送短信分为两种方式
一、sdk调用
using mas.ecloud.sdkclient; using System; namespace 短信发送 { class Program { private static Client client = Client.instance; static void Main(string[] args) { string url = "";//身份认证地址,向客户经理获取。 string apId = "";//账号 string secretKey = "";//密码 string ecName = ""; //集团名称 string sign = ""; //网关签名编码 string mobiles = "";//电话号码 string content = "测试短信发送";//短信内容 bool loginResult = client.login(url, apId, secretKey, ecName); Console.WriteLine("登录结果:" + loginResult); if (loginResult) { // 发送短信 int sendResult = client.sendDSMS(new string[] { mobiles }, content, "", 5, sign, Guid.NewGuid().ToString()); Console.WriteLine("发送结果:" + sendResult); } } } }
二、http调用
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Net; using System.Net.Http; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace 短信发送 { public class SMSUtils { private static String apId = "";//用户名 private static String secretKey = "";//密码 private static String ecName = ""; //集团名称 private static String sign = ""; //网关签名编码 private static String addSerial = ""; //拓展码 填空 public static String url = "http://112.35.1.155:1992/sms/norsubmit";//请求urlhttp://112.35.1.155:1992/sms/norsubmit private static String mobiles = "";//电话号码 private static String content = "测试短信发送";//短信内容 public SMSUtils() { } public void SendMessage() { SendReq sendReq = new SendReq(); sendReq.apId = apId; sendReq.ecName = ecName; sendReq.secretKey = secretKey; sendReq.content = content; sendReq.mobiles = mobiles; sendReq.addSerial = addSerial; sendReq.sign = sign; sendReq.mac = UserMd5(sendReq.ecName + sendReq.apId + sendReq.secretKey + sendReq.mobiles + sendReq.content + sendReq.sign); var reqText = JsonConvert.SerializeObject(sendReq); string encode = Convert.ToBase64String(Encoding.UTF8.GetBytes(reqText)); sendPost(url, encode); } #region MyRegion private static void sendPost(string url, string param) { //创建HttpClient(注意传入HttpClientHandler) var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip }; using (var http = new HttpClient(handler)) { var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(param), Encoding.UTF8, "application/json"); var response = http.PostAsync(url, content).Result;//传参使用 response.EnsureSuccessStatusCode(); //确保HTTP成功状态值R //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip) var result = response.Content.ReadAsStringAsync().Result; var json = JsonConvert.DeserializeObject<SendRes>(result); bool success = json.success; } } #endregion //MD5加密 public string UserMd5(string str) { string cl = str; string pwd = ""; MD5 md5 = MD5.Create();//实例化一个md5对像 // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); // 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得 for (int i = 0; i < s.Length; i++) { // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 pwd = pwd + s[i].ToString("x2"); } return pwd; } } /** * 发送短信请求实体 */ public class SendReq { public string ecName { get; set; } //集团客户名称 public string apId { get; set; } //用户名 public string secretKey { get; set; } //密码 public string mobiles { get; set; } //手机号码逗号分隔。(如“18137282928,18137282922,18137282923”) public string content { get; set; } //发送短信内容 public string sign { get; set; } //网关签名编码,必填,签名编码在中国移动集团开通帐号后分配,可以在云MAS网页端管理子系统-SMS接口管理功能中下载。 public string addSerial { get; set; } //扩展码,根据向移动公司申请的通道填写,如果申请的精确匹配通道,则填写空字符串(""),否则添加移动公司允许的扩展码。 public string mac { get; set; } //API输入参数签名结果,签名算法:将ecName,apId,secretKey,mobiles,content ,sign,addSerial按照顺序拼接,然后通过md5(32位小写)计算后得出的值。 } /** * 发送短信响应实体 */ public class SendRes { public string rspcod { get; set; } //响应状态码 public string msgGroup { get; set; } //消息批次号,由云MAS平台生成,用于验证短信提交报告和状态报告的一致性(取值msgGroup)注:如果数据验证不通过msgGroup为空 public bool success { get; set; } //数据校验结果 } }
原文地址:https://www.cnblogs.com/macT/p/11350760.html
时间: 2024-10-29 19:10:15