标签:
最近在做一个学校短信项目,使用了第三方提供的短信接口,现将本次封装好的短信接口帮助类分享一下:
下面是发送界面,还未美化完,支持学校各种形式的短信发送功能。
下面是封装好的方法,支持余额查询,批量发送等多种功能。
/// <summary> /// 短信接口帮助类 /// /// /// 修改纪录 /// /// 2016-07-02 版本:1.0 SongBiao 创建文件。 /// /// <author> /// <name>SongBiao</name> /// <date>2016-07-02</date> /// </author> /// </summary> public class MobilleMessageHelper { /// <summary> /// 短信服务地址 /// </summary> const string Url = "http://XXXX.com/api/MsgSend.asmx"; /// <summary> /// 短信账号 /// </summary> const string UserCode = "xxxxx"; /// <summary> /// 短信密码 /// </summary> const string UserPass = "yyyyy"; /// <summary> /// 签名 /// </summary> public const string Signature = "【宜宾鸿辉】"; /// <summary> /// 通道号 /// </summary> const string Channel="178"; /// <summary> /// 扩展号 /// </summary> const string ExeNo="扩展号"; #region http 请求及加密 /// <summary> /// http get请求 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <returns></returns> public static string HttpGet(string url, string data) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + data); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } catch (Exception ex) { return ex.Message; } } /// <summary> /// http post请求 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <returns></returns> public static string HttpPost(string url, string data) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; byte[] info = Encoding.UTF8.GetBytes(data); using (Stream stream = request.GetRequestStream()) { stream.Write(info, 0, info.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } catch (Exception ex) { return ex.Message; } } /// <summary> /// DES加密 /// </summary> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static string EncodeStr(string str, string key) { try { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8)); provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8)); byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(str); MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write); stream2.Write(bytes, 0, bytes.Length); stream2.FlushFinalBlock(); StringBuilder builder = new StringBuilder(); foreach (byte num in stream.ToArray()) { builder.AppendFormat("{0:X2}", num); } stream.Close(); return builder.ToString(); } catch (Exception ex) { return ex.Message; } } /// <summary> /// //SHA1加密 /// </summary> /// <param name="strSha1In"></param> /// <returns></returns> public static string SHA1Hash(string strSha1In) { SHA1 sha1 = new SHA1CryptoServiceProvider(); byte[] bytesSha1In = UTF8Encoding.Default.GetBytes(strSha1In); byte[] bytesSha1Out = sha1.ComputeHash(bytesSha1In); string strSha1Out = BitConverter.ToString(bytesSha1Out); strSha1Out = strSha1Out.Replace("-", ""); return strSha1Out; } #endregion /* 返回值类型 string 成功返回值 返回用户金额的数字 失败返回值 返回带负号的失败代码 */ #region 余额查询 /// <summary> /// POST方式请求 余额查询 /// </summary> /// <returns></returns> public static string GetBalanceByPost() { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass); string result = HttpPost(Url + "/GetBalance", sb.ToString()); return result; } /// <summary> /// Get方式请求 余额查询 /// </summary> /// <returns></returns> public static string GetBalanceByGet() { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass); string result = HttpGet(Url + "/GetBalance", sb.ToString()); return result; } //private void getbalance() //{ // TopenServiceReference.MsgSendSoapClient topen = new TopenServiceReference.MsgSendSoapClient(); // string userName = "用户名"; // string passWord = "密码"; // string sendResult = topen.GetBalance(userName, passWord); // //然后,根据返回的sendResult作相应处理 //} #endregion /* 返回值类型 string 成功返回值 返回本次提交的批次号(如:2114355899380234221),(或叫序列号、流水号等,提交一次返回一个) 失败返回值 返回带负号的失败代码 */ #region 短信发送 /// <summary> /// POST方式请求 短信发送 手机号码,多个号码用英文半角逗号分隔例如:13900000000,13900000001每次提交不多于500个号码 /// </summary> /// <param name="desNo"></param> /// <param name="msg"></param> /// <returns>返回本次提交的批次号(如:2114355899380234221),(或叫序列号、流水号等,提交一次返回一个) ;失败返回值 返回带负号的失败代码</returns> public static string SendMsgByPost(string desNo, string msg) { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass+"&"); sb.Append("DesNo="+desNo+"&"); sb.Append("Msg="+msg+Signature+"&"); sb.Append("Channel="+Channel); string result = HttpPost(Url + "/sendMes", sb.ToString()); return result; } /// <summary> /// Get方式请求 短信发送 手机号码,多个号码用英文半角逗号分隔例如:13900000000,13900000001每次提交不多于500个号码 /// </summary> /// <param name="desNo"></param> /// <param name="msg"></param> /// <returns>返回本次提交的批次号(如:2114355899380234221),(或叫序列号、流水号等,提交一次返回一个) ;失败返回值 返回带负号的失败代码</returns> public static string SendMsgByGet(string desNo, string msg) { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass+"&"); sb.Append("DesNo="+desNo+"&"); sb.Append("Msg="+msg+Signature+"&"); sb.Append("Channel="+Channel); string result = HttpGet(Url + "/sendMes", sb.ToString()); return result; } /// <summary> /// /POST方式请求 加密短信发送 手机号码,多个号码用英文半角逗号分隔例如:13900000000,13900000001每次提交不多于500个号码 /// </summary> /// <param name="desNo"></param> /// <param name="msg"></param> /// <returns>返回本次提交的批次号(如:2114355899380234221),(或叫序列号、流水号等,提交一次返回一个) ;失败返回值 返回带负号的失败代码</returns> public static string SendMsgEncryptByPost(string desNo, string msg) { string encryptCode = EncodeStr( "userPass=" + UserPass + "&DesNo=" + desNo + "&Msg=" + msg + Signature + "&Channel=" + Channel, SHA1Hash(UserPass)); StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("submitInfo="+encryptCode); string result = HttpPost(Url + "/sendMsgByEncrypt", sb.ToString()); return result; } /// <summary> /// Get方式请求 加密短信发送 手机号码,多个号码用英文半角逗号分隔例如:13900000000,13900000001每次提交不多于500个号码 /// </summary> /// <param name="desNo"></param> /// <param name="msg"></param> /// <returns>返回本次提交的批次号(如:2114355899380234221),(或叫序列号、流水号等,提交一次返回一个) ;失败返回值 返回带负号的失败代码</returns> public static string SendMsgEncryptByGet(string desNo, string msg) { string encryptCode=EncodeStr("userPass="+UserPass+"&DesNo="+desNo+"&Msg="+msg+Signature+"&Channel="+Channel,SHA1Hash(UserPass)); StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("submitInfo="+encryptCode); string result = HttpGet(Url + "/sendMsgByEncrypt", sb.ToString()); return result; } /// <summary> /// POST方式请求 个性化短信发送 /// 例如,需发送如下短信 /// 13800000000 王某某,明天记得要开会【123】 /// 13800000000 张某某,明天记得要开会【123】 /// 13800000000 李某某,明天记得要开会【123】 /// 最终拼接url如下:http://yes.itissm.com/api/IndividualSm.aspx?account=admin1&pwd=123&channel=37&msg=13800000000|!|王某某,明天记得要开会【123】|^|13900000000|!|张某某,明天记得要开会【123】|^|18600000000|!|李某某,明天记得要开会【123】 /// </summary> /// <param name="msg">短信内容为utf-8编码每条个性化短信以英文|^|分割,号码与短信内容以英文|!|分割</param> /// <returns>返回本次提交的批次号(如:2114355899380234221),(或叫序列号、流水号等,提交一次返回一个) ;失败返回值 返回带负号的失败代码</returns> public static string IndividUalsmByPost(string msg) { StringBuilder sb = new StringBuilder(); sb.Append("account="+UserCode+"&"); sb.Append("pwd="+UserPass+"&"); sb.Append("Channel="+Channel+"&"); //sb.Append("Msg=手机号|!|短信内容【签名】|^|手机号|!|短信内容【签名】|^|"); sb.Append("Msg="+msg); string result = HttpPost(Url,sb.ToString()); return result; } //private void SendSms() //webservice请求方式 //{ // TopenServiceReference.MsgSendSoapClient topen = new TopenServiceReference.MsgSendSoapClient(); // string userName = "用户名"; // string passWord = "密码"; // string mobiles = "13900000000,13800000000,13100000000,……"; // string msgContent = "短信内容(含签名)"; // string channel = "由拓鹏给您的通道编号"; // string sendResult = topen.sendMes(userName, passWord, mobiles, msgContent, channel); //此处的sendMes可能因接口文档不同而不同,请注意。返回批次号,可保存下来,作为获取发送报告凭据 // //然后,根据返回的sendResult作相应处理 //} #endregion #region 短信扩展发送 /// <summary> /// POST方式请求 短信扩展发送 /// </summary> /// <returns></returns> public static string SendMsgExtByPost(string desNo,string msg) { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass+"&"); sb.Append("DesNo="+desNo+"&"); sb.Append("Msg="+msg+Signature+"&"); sb.Append("Channel="+Channel+"&"); sb.Append("ExeNo="+ExeNo); string result = HttpPost(Url + "/sendMesExt", sb.ToString()); return result; } /// <summary> /// Get方式请求 短信扩展发送 /// </summary> /// <returns></returns> public static string SendMsgExtByGet(string desNo,string msg) { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass+"&"); sb.Append("DesNo="+desNo+"&"); sb.Append("Msg="+msg+Signature+"&"); sb.Append("Channel="+Channel+"&"); sb.Append("ExeNo="+ExeNo); string result = HttpGet(Url + "/sendMesExt", sb.ToString()); return result; } #endregion #region 获取状态报告 /// <summary> /// POST方式请求 获取状态报告 /// </summary> /// <returns></returns> public static string GetReport2ByPost() { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass); string result = HttpPost(Url + "/GetReport2", sb.ToString()); return result; } /// <summary> /// Get方式请求 获取状态报告 /// </summary> /// <returns></returns> public static string GetReport2ByGet() { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass); string result = HttpGet(Url + "/GetReport2", sb.ToString()); return result; } /// <summary> /// POST方式请求加密 获取状态报告 /// </summary> /// <returns></returns> public static string GetReport2EncryptByPost() { string encryptCode=EncodeStr("userPass="+UserPass,SHA1Hash(UserPass)); StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("submitInfo="+encryptCode); string result = HttpPost(Url + "/GetReport2ByEncrypt", sb.ToString()); return result; } /// <summary> /// Get方式请求加密 获取状态报告 /// </summary> /// <returns></returns> public static string GetReport2EncryptByGet() { string encryptCode=EncodeStr("userPass="+UserPass,SHA1Hash(UserPass)); StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("submitInfo="+encryptCode); string result = HttpGet(Url + "/GetReport2ByEncrypt", sb.ToString()); return result; } #endregion #region 获取回复 /// <summary> /// POST方式请求 获取回复 /// </summary> /// <returns></returns> public static string GetMo2ByPost() { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass); string result = HttpPost(Url + "/GetMo2", sb.ToString()); return result; } /// <summary> /// Get方式请求 获取回复 /// </summary> /// <returns></returns> public static string GetMo2ByGet() { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass+"&"); string result = HttpGet(Url + "/GetMo2", sb.ToString()); return result; } /// <summary> /// POST方式请求 获取回复(加密) /// </summary> /// <returns></returns> public static string GetMo2EncryptByPost() { string encryptCode=EncodeStr("userPass="+UserPass,SHA1Hash(UserPass)); StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("submitInfo="+encryptCode); string result = HttpPost(Url + "/GetMo2ByEncrypt", sb.ToString()); return result; } /// <summary> /// Get方式请求 获取回复(加密) /// </summary> /// <returns></returns> public static string GetMo2EncryptByGet() { string encryptCode = EncodeStr("userPass=密码", SHA1Hash(UserPass)); StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("submitInfo="+encryptCode); string result = HttpGet(Url + "/GetMo2ByEncrypt", sb.ToString()); return result; } #endregion #region 获取关键字 /// <summary> /// POST方式请求 获取关键字 /// </summary> /// <returns></returns> public static string GetKeywordsByPost() { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass+"&"); sb.Append("Channel="+Channel); string result = HttpPost(Url + "/GetKeyWords", sb.ToString()); return result; } /// <summary> /// Get方式请求 获取关键字 /// </summary> /// <returns></returns> public static string GetKeywordsByGet() { StringBuilder sb = new StringBuilder(); sb.Append("userCode="+UserCode+"&"); sb.Append("userPass="+UserPass+"&"); sb.Append("Channel="+Channel); string result = HttpGet(Url + "/GetKeyWords", sb.ToString()); return result; } #endregion } /// <summary> /// 手机短信 云信状态枚举 /// </summary> public enum YunXinMessageStatus { /// <summary> /// -1 应用程序异常 /// </summary> [EnumDescription("应用程序异常")] ApplicationError = -1, /// <summary> /// -3 用户名密码错误或者用户无效 /// </summary> [EnumDescription("用户名密码错误或者用户无效")] AccountError = -3, /// <summary> /// -5 签名不正确(格式为:XXX【签名内容】), 注意:短信内容最后一个字符必须是】 /// </summary> [EnumDescription("签名不正确")] SignatureError = -5, /// <summary> /// -6:keyWords 含有关键字keyWords(keyWords为敏感内容,如:-6:促销) /// </summary> [EnumDescription("含有关键字")] KeyWordsNotAllow = -6, /// <summary> /// -7 余额不足 /// </summary> [EnumDescription("余额不足")] BalanceNotEnough = -7, /// <summary> /// -8 没有可用的通道,或不在时间范围内 /// </summary> [EnumDescription("余额不足")] ChannelNoAvailable = -8, /// <summary> /// -9 发送号码一次不能超过1000个 /// </summary> [EnumDescription("发送号码一次不能超过1000个")] SendTooManyNumbers = -9, /// <summary> /// -10 号码数量大于允许上限(不设置上限时,不可超过1000) /// </summary> [EnumDescription("号码数量大于允许上限(不设置上限时,不可超过1000)")] SendOverLimit = -10, /// <summary> /// -11 号码数量小于允许下限 /// </summary> [EnumDescription("号码数量小于允许下限")] SendLessLimit = -11, /// <summary> /// -12 模板不匹配 /// </summary> [EnumDescription("模板不匹配")] TemplateMismatch = -12, /// <summary> /// -13 Invalid Ip ip绑定用户,未绑定该ip /// </summary> [EnumDescription("ip绑定用户,未绑定该ip")] InvalidIp = -13, /// <summary> /// -14 用户黑名单 /// </summary> [EnumDescription("用户黑名单")] UserBlackList = -14, /// <summary> /// -15 系统黑名单 /// </summary> [EnumDescription("系统黑名单")] SystemBlackList = -15, /// <summary> /// -16 号码格式错误 /// </summary> [EnumDescription("号码格式错误")] NumberFormatError = -16, /// <summary> /// -17 无效号码(格式正常,可不是正确的电话号码,如12345456765) /// </summary> [EnumDescription("无效号码")] InvalidNumber = -17, /// <summary> /// -18 没有设置用户的固定下发扩展,不能自定义扩展 /// </summary> [EnumDescription("没有设置用户的固定下发扩展,不能自定义扩展")] CannotCustomizeExtension = -18, /// <summary> /// -19 强制模板通道,不能使用个性化接口 /// </summary> [EnumDescription("强制模板通道,不能使用个性化接口")] CanNotUsePersonalized = -19, /// <summary> /// -20 包含非法字符 /// </summary> [EnumDescription("包含非法字符")] ContainsIllegalCharacters = -20, /// <summary> /// -21 没有找到对应的submitID设置 /// </summary> [EnumDescription("没有找到对应的submitID设置")] NoSubmitID = -21, /// <summary> /// -22 解密失败 /// </summary> [EnumDescription("解密失败")] DecryptionFailure = -22, /// <summary> /// -23 查询余额过频繁(至少间隔10秒) /// </summary> [EnumDescription("查询余额过频繁(至少间隔10秒)")] QueryBalanceTooFrequent = -23, }
标签:
原文地址:http://www.cnblogs.com/hnsongbiao/p/5637925.html