通过移动的Mas接口发送短信
1. 首先,需要移动公司提供的用户名、密码、服务ID、接口Url等信息。
2. 将短信信息整理成XML格式的字符串,再转为byte数组,通过POST的方式,将短信发往Mas接口。需要引用"MSXML2"组件。
注意:发往Mas接口的byte数组的编码方式需要设置为"GBK",否则收到的短信将会是乱码。
byte[] data = System.Text.Encoding.GetEncoding("GBK").GetBytes(DecodeString);
3. 实现代码如下:
/// <summary>
/// 移动公司的Mas短信接口
/// </summary>
public class MasSerVice
{
string userID = System.Configuration.ConfigurationManager.AppSettings["userID"].ToString(); //用户名
string passWord = System.Configuration.ConfigurationManager.AppSettings["passWord"].ToString(); //密码
string serviceID = System.Configuration.ConfigurationManager.AppSettings["ServiceID"].ToString(); //服务ID
string postUrl = System.Configuration.ConfigurationManager.AppSettings["postUrl"].ToString(); //移动提供的Mas接口的Url /// <summary>
/// 准备发送的XML字符串
/// </summary>
/// <param name="userID"></param>
/// <param name="passWd"></param>
/// <param name="mobileNo">手机号码,如果有多个号码,号码之间按逗号隔开,不超过100个</param>
/// <param name="smsContent">短信内容</param>
/// <returns></returns>
public string PrepareXml(string MobileNo, string smsContent)
{
string str = String.Format("<?xml version=\"1.0\" encoding=\"GB2312\"?>" +
"<svc_init ver=\"2.0.0\">" +
"<sms ver=\"2.0.0\">" +
@"<client>
<id>{0}</id>
<pwd>{1}</pwd>
<serviceid>{2}</serviceid>
</client>
<sms_info>
<phone>{3}</phone>
<content>{4}</content>
</sms_info>
</sms>
</svc_init>", userID, passWord, serviceID, MobileNo, smsContent); return str;
} /// <summary>
/// 发送短信
/// </summary>
/// <param name="mobileNo">手机号码,如果有多个号码,号码之间按逗号隔开,不超过100个</param>
/// <param name="smsContent">短信内容</param>
/// <returns></returns>
public string MessageSend(string mobileNo, string smsContent)
{
string result = String.Empty;
string postString = PrepareXml(mobileNo.Replace(',', ',').TrimEnd(new char[] { ',' }), smsContent);
try
{
string DecodeString = postString.Replace(" ", "+"); byte[] data = System.Text.Encoding.GetEncoding("GBK").GetBytes(DecodeString);
MSXML2.ServerXMLHTTP xmlhttp = new MSXML2.ServerXMLHTTP(); xmlhttp.open("POST", postUrl, false, null, null);
xmlhttp.setRequestHeader("Content-Length", data.Length.ToString());
xmlhttp.setRequestHeader("Content-Type", "text/xml;charset=gb2312");
xmlhttp.send(data);
xmlhttp.waitForResponse(); Byte[] b = (Byte[])xmlhttp.responseBody; //返回数据 string backXml = System.Text.Encoding.GetEncoding("gb2312").GetString(b); System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.LoadXml(backXml);
result = xmldoc.GetElementsByTagName("retcode")[].InnerText;
} catch (Exception ex)
{
result = ex.Message;
}
return result;
}
}
4. 调用接口:
new MasService().MessageSend("", "短信内容……");
如果返回的值为"00",即表示短信已经发送成功。
附:retmesg为返回代码说明信息
|
retcode返回值 |
说明 |
|
00 |
提交成功 |
|
01 |
手机号码数量和sms_id数量不匹配 |
|
26 |
超过短信最大发送量 |
|
25 |
手机号码格式有误 |
|
24 |
phone,content,sms_id标签内容为空 |
|
23 |
sms_info标签内容为空 |
|
22 |
账户认证失败 |
|
21 |
账号信息为空 |
|
12 |
client标签内容为空 |
|
11 |
提交的内容空 |
|
14 |
数据库连接失败 |
通过移动的Mas接口发送短信的更多相关文章
- 企业短信通 C# HTTP接口 发送短信
/* 功能: 企业短信通 C# HTTP接口 发送短信 修改日期: 2014-09-01 说明: http://api.cnsms.cn/?ac=send&uid=用户账号&pwd=M ...
- android 中调用接口发送短信
android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getD ...
- 注册登录页面修订-Python使用redis-手机验证接口-发送短信验证
登录页面修订 views.Login.vue <template> <div class="login box"> <img src="@/ ...
- JAVA 调用第三方短信平台接口发送短信
做了几个调用三方短信平台发送短信的例子,大部分需要 携带参数,向指定URL发送请求 回顾对接第一个平台时痛苦的乱码经历,这里放一份代码,算是个模版,再用到的时候过来copy一下就OK. 在进入主题之前 ...
- zabbix3调用接口发送短信告警
一.需求 之前使用的邮件告警,由于经常会忽略邮件,所以有时候告警查看的并不及时,所以增加短信告警的,以便及时处理. 二.zabbix-server端的配置 # 需要在zabbix-server端打开A ...
- java 调用短信 api 接口发送短信
参考: https://blog.csdn.net/u014793522/article/details/59062014 参考 :https://blog.csdn.net/Lu_shilusi ...
- 如何使用微信小程序云函数发送短信验证码
其实微信小程序前端和云端都是可以调用短信平台接口发送短信的,使用云端云函数的好处是无需配置域名,也没有个数限制. 本文使用的是榛子云短信平台(http://smsow.zhenzikj.com) ,S ...
- Java发送短信
1.接口使用介绍 发送短信肯定需要使用第三方接口,Java本身是肯定不能直接发送短信的.第三方接口有很多,这里直接找个正规靠谱一点的学习一下 这里使用了中国网建(http://sms.webchine ...
- 发送短信——java
闲来无事研究一下调用第三方接口发送短信的技术 这一次我们使用阿里的短信服务 一.进行平台相关服务的注册和设置 下面请参照阿里的短信服务文档进行设置,只要按照文档步骤来差不多30分钟就能搞定服务注册: ...
随机推荐
- C#实现打印与打印预览功能
C#实现打印与打印预览功能的思路及代码. 在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的 ...
- iomanip,setw(),setw: undeclared identifier
今天使用setw(),提示setw: undeclared identifier,上网查了下,原来是没有包含头文件iomanip,现摘录如下: iomanip #include <iomanip ...
- 1059. Prime Factors (25)
时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, y ...
- 【BZOJ2199】 [Usaco2011 Jan]奶牛议会
Description 由于对Farmer John的领导感到极其不悦,奶牛们退出了农场,组建了奶牛议会.议会以“每头牛 都可以获得自己想要的”为原则,建立了下面的投票系统: M只到场的奶牛 (1 & ...
- 使用MyBatis的resultMap高级查询时常用的方式总结
以下内容已经通过楼主测试, 从pd设计数据库到测试完成, 之前楼主也没有过Mybatis 使用resultMap觉得有点乱,最近抽出时间总结了一下也算对MyBatis的resultMap进行一次系统的 ...
- linux查找某个文件中单词出现的次数
文件名称:list 查找单词名称:test 操作命令: (1)more list | grep -o test | wc -l (2)cat list | grep -o test | wc -l ( ...
- 【BZOJ 1069】 凸包+旋转卡壳
1069: [SCOI2007]最大土地面积 Description 在某块平面土地上有N个点,你可以选择其中的任意四个点,将这片土地围起来,当然,你希望这四个点围成的多边形面积最大. Input 第 ...
- Oracle导入(imp )与导出(exp )
导出exp username/password@orcl file=db.dmp 导入imp username/password@orcl file=h:\db.dmp full=y 备注:在导入之 ...
- 李洪强iOS开发之【零基础学习iOS开发】【02-C语言】07-基本数据类型
C语言有丰富的数据类型,因此它很适合用来编写数据库,如DB2.Oracle等大型数据库都是C语言写的.其中,提供了4种最常用的基本数据类型:char.int.float.double,使用这些数据类型 ...
- NSString的常用方法
创建一个新字符串并将其设置为 path 指定的文件的内容,使用字符编码enc,在error上返回错误 + (id)stringWithContentsOfURL:(NSURL *)url encodi ...