云MAS提供webservice服务,接收客户端向云MAS平台发送请求,带上相应的请求参数,云MAS平台接收请求,并进行验证,验证通过后进行短信发送。
本文实现一对多短信发送
接口文档:中国移动云mas平台 webservice
接口设置
- 登陆云MAS管理平台
平台地址:http://mas.10086.cn/login - 设置或者新增接口
查看接口设置,协议是否为“WS”,如图
此处用户名、密码、所属分组对应接口入参的apId、secretKey、ecName
下载签名sign
如下图
VS代码
-
添加WSDL链接
引用->添加服务引用
“链接”处粘贴:http://112.35.10.201:1999/smsservice?wsdl,如下图
-
添加MD5加密
注意设置编码:byte[] result = Encoding.UTF8.GetBytes(data)
MD5Util.cs
using System;
using System.Security.Cryptography;
using System.Text;
namespace SMS
{
class MD5Util
{
public static string retMD5(string[] args)
{
string apId =args[0];
string secretKey= args[1];
string ecName = args[2];
string mobile = args[3];
string content = args[4];
string sign = args[5];
string addSerial = args[6];
string data = ecName + apId + secretKey + mobile + content + sign + addSerial;
byte[] result = Encoding.UTF8.GetBytes(data);
MD5 md5 = new MD5CryptoServiceProvider();
byte[] output = md5.ComputeHash(result);
string mac = BitConverter.ToString(output).Replace("-", "").ToLower();
return mac;
}
}
}
- 添加请求类
SendReq.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SMS
{
class SendReq
{
/**集团客户名称*/
public String ecName { set; get; }
/**用户名*/
public String apId { set; get; }
/**密码*/
public String secretKey { set; get; }
/**手机号码逗号分隔。(如“1813728****,1813728****,1813728****”)*/
public String mobiles { set; get; }
/**发送短信内容*/
public String content { set; get; }
/**网关签名编码,必填,签名编码在中国移动集团开通帐号后分配,可以在云MAS网页端管理子系统-SMS接口管理功能中下载。*/
public String sign { set; get; }
/**扩展码,根据向移动公司申请的通道填写,如果申请的精确匹配通道,则填写空字符串(""),否则添加移动公司允许的扩展码。*/
public String addSerial { set; get; }
/**按文档要求加密后的mac*/
public String mac { set; get; }
}
}
- 发送短信
private void sendSms(string mob,string cont)
{
//本接口只实现了单个手机号发送,多个手机号请自行拼接加密mac和入参xml
SendReq sr = new SendReq();
SendRes ss = new SendRes();
sr.apId = "your apid";
sr.ecName = "your ecNmae";
sr.secretKey = "your secretkey";
sr.sign = "your sign";
sr.addSerial = "your addserial";
sr.content = cont;//短信内容
sr.mobiles = mob;//手机号
string[] str = new string[7];
str[2] = sr.ecName;
str[0] = sr.apId;
str[1] = sr.secretKey;
str[3] = sr.mobiles;
str[4] = sr.content;
str[5] = sr.sign;
str[6] = sr.addSerial;
sr.mac = MD5Util.retMD5(str);
sms.WsSmsServiceClient smsService = new sms.WsSmsServiceClient();
string xml = "<?xml version=\"1.0\" encoding=\"utf - 8\"?>" +
// string xml= "" +
"<WsSubmitReq>" +
"<apId>" + sr.apId + "</apId>" +
"<secretKey>" + sr.secretKey + "</secretKey>" +
"<ecName>" + sr.ecName + "</ecName>" +
"<mobiles>" +
"<string>" + sr.mobiles + "</string>" +
"</mobiles>" +
"<content>" + sr.content + "</content>" +
"<sign>" + sr.sign + "</sign>" +
"<addSerial>" + sr.addSerial + "</addSerial>" +
"<mac>" + sr.mac + "</mac>" +
"</WsSubmitReq>";
MessageBox.Show(xml);
//byte[] bytes = Encoding.UTF8.GetBytes(xml);
// Convert.ToBase64String(bytes);
string result = smsService.sendSms(xml);
MessageBox.Show(result);
}
源码
VS2019 c# 中国移动云mas平台 webservice实现源码
常见问题
- InvalidUsrOrPwd,接口协助不是“WS”,apId、secretKey、ecName传值错误
- MD5加密没有设置编码“UTF-8”,byte[] result = Encoding.UTF8.GetBytes(data)
- MD5要转换为32位小写,BitConverter.ToString(output).Replace(“-”, “”).ToLower();
- 一直解决不了,云MAS平台登陆界面添加客服微信,这个效率还不错,