项目集成短信总结

川长思鸟来 2022-03-27 13:16 279阅读 0赞

短信接口

  1. 项目需要集成短信功能。网上查了很多资料,了解到有下面几种短信途径。
  2. 1. 可以通过移动等运营商购买短信接口
  3. 2. 可以通过第三方短信提供商。如短信通([http://www.dxton.com/][http_www.dxton.com])。
  4. 前段时间和湖南短信提供商合作,不过不顺利,影响了项目的进度。最开始商量的是我们自己建立数据库,然后他们提供插件。我们在网上买了空间,可是需求理解错了。后来需要在他们的服务器上建立数据库,然后插入数据。最后由于多种原因,没有成功。附部分工作(按照《DB新建插件.doc》在MAS移动服务代理器后台管理中新建插件;购买数据库空间,按照《短信DB开发手册.doc》建立数据库,插入测试数据)。
  5. 后来在网上找了一家短信提供商,名叫“短信通”。最开始提供的接口很容易实现,他们还提供了测试账号。集成到项目里了,可是测试账号的短信数量不够。于是向他们购买账号。可是又遇到了问题,他们提供两种短信,一种是群发,适用:5万条内、小批量群发短信客户,资费:1001500条;3005000条;5001万条,试用满意后1-500元任意充值(通过网银、支付宝),但不能集成到项目里,只能手动编辑。另一种是短信接口,可以集成到项目里,但是1万条起订,由于我们的项目处于测试阶段,用不了那么多。后来几经周折,他们提供一个账号,1001500条短信。后来经过小小的改动后,算是顺利集成到项目里了。
  6. 下面提供自己在原有接口上改动的代码(未集成Junit进行单元测试):

普通短信接口(提供API改动版)

PropertiesConfig.java

  1. package com.axt.sms.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.FileInputStream;
  4. import java.io.InputStream;
  5. import java.util.Properties;
  6. /**
  7. *
  8. * @author Wentasy
  9. * @createtime 2013年1月13日14:58:54
  10. * @version 1.0
  11. * @since JDK 1.6
  12. */
  13. public class PropertiesConfig {
  14. /**
  15. * 获取整个配置文件中的属性
  16. * @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties
  17. */
  18. public static Properties readData(String filePath) {
  19. filePath = getRealPath(filePath);
  20. Properties props = new Properties();
  21. try {
  22. InputStream in = new BufferedInputStream(new FileInputStream(filePath));
  23. props.load(in);
  24. in.close();
  25. return props;
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. return null;
  29. }
  30. }
  31. private static String getRealPath(String filePath) {
  32. //获取绝对路径 并截掉路径的”file:/“前缀
  33. return PropertiesConfig.class.getResource("/" + filePath).toString().substring(6);
  34. }
  35. }

SmsUtil.java

  1. package com.axt.sms.util;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.net.URLEncoder;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Properties;
  10. /**
  11. * @className: SmsUtil.java
  12. * @classDescription:
  13. * @function:
  14. * @author: Wentasy
  15. * @createTime: 2013-1-13 上午10:50:41
  16. * @modifyTime:
  17. * @modifyReason:
  18. * @since: JDK 1.6
  19. */
  20. public class SmsUtil {
  21. @SuppressWarnings("deprecation")
  22. public static String sendSms(String content,String phoneNumber[]) throws Exception{
  23. String reslutCode = "";
  24. String mobile = "";
  25. //发送内容
  26. // content = "短信通JAVA示例测试";
  27. // 创建StringBuffer对象用来操作字符串
  28. StringBuffer sb = new StringBuffer("http://http.chinasms.com.cn/tx/?");
  29. Properties props = PropertiesConfig.readData("sms.properties");
  30. String uid = props.getProperty("uid");
  31. String pwd = props.getProperty("pwd");
  32. // 向StringBuffer追加用户名
  33. sb.append("uid="+uid);
  34. // 向StringBuffer追加密码(密码采用MD5 32位 小写)
  35. sb.append("&pwd="+pwd);
  36. // 向StringBuffer追加手机号码
  37. for (int i = 0; i < phoneNumber.length; i++) {
  38. if(i != phoneNumber.length - 1){
  39. mobile += phoneNumber[i] + ",";
  40. }else{
  41. mobile += phoneNumber[i] + "";
  42. }
  43. }
  44. sb.append("&mobile="+mobile);
  45. // 向StringBuffer追加消息内容转URL标准码
  46. sb.append("&content="+URLEncoder.encode(content));
  47. // 创建url对象
  48. URL url = new URL(sb.toString());
  49. // 打开url连接
  50. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  51. // 设置url请求方式 'get' 或者 'post'
  52. connection.setRequestMethod("POST");
  53. // 发送
  54. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  55. // 返回发送结果
  56. reslutCode = in.readLine();
  57. // 返回结果为'100' 发送成功
  58. System.out.println(reslutCode);
  59. return reslutCode;
  60. }
  61. public static void main(String[] args) throws Exception{
  62. // String phoneNumber[] = new String[1];
  63. // phoneNumber[0] = "152XXXXXXXX";
  64. // phoneNumber[1] = "135XXXXXXXX";
  65. // String content = "短信通JAVA示例测试3";
  66. // String mobile = "";
  67. // for (int i = 0; i < phoneNumber.length; i++) {
  68. // if(i != phoneNumber.length - 1){
  69. // mobile += phoneNumber[i] + ",";
  70. // }else{
  71. // mobile += phoneNumber[i] + "";
  72. // }
  73. // }
  74. // System.out.println(mobile);
  75. // new SmsUtil().sendSms(content, phoneNumber);
  76. // new SmsUtil().pringResultCode("100");
  77. List<String> list = new ArrayList<String>();
  78. list.add("123456");
  79. list.add("123457");
  80. // String str[] = listToStringArray(list);
  81. //
  82. // for (int i = 0; i < str.length; i++) {
  83. // System.out.println(str[i]);
  84. // }
  85. // String mobile = "";
  86. // for (int i = 0; i < str.length; i++) {
  87. // if(i != str.length - 1){
  88. // mobile += str[i] + ",";
  89. // }else{
  90. // mobile += str[i] + "";
  91. // }
  92. // }
  93. //
  94. // System.out.println(mobile);
  95. }
  96. public static String pringResultCode(String resultCode){
  97. String reason = "";
  98. if(resultCode.equals("100")){
  99. reason = "发送成功";
  100. System.out.println("发送成功");
  101. }else if(resultCode.equals("101")){
  102. reason = "发送成功";
  103. System.out.println("验证失败");
  104. }else if(resultCode.equals("102")){
  105. reason = "发送成功";
  106. System.out.println("短信不足");
  107. }else if(resultCode.equals("103")){
  108. reason = "发送成功";
  109. System.out.println("操作失败");
  110. }else if(resultCode.equals("104")){
  111. reason = "非法字符";
  112. System.out.println("非法字符");
  113. }else if(resultCode.equals("105")){
  114. reason = "内容过多";
  115. System.out.println("内容过多");
  116. }else if(resultCode.equals("106")){
  117. reason = "号码过多";
  118. System.out.println("号码过多");
  119. }else if(resultCode.equals("107")){
  120. reason = "频率过快";
  121. System.out.println("频率过快");
  122. }else if(resultCode.equals("108")){
  123. reason = "号码内容空";
  124. System.out.println("号码内容空");
  125. }else if(resultCode.equals("109")){
  126. reason = "账号冻结";
  127. System.out.println("账号冻结");
  128. }else if(resultCode.equals("110")){
  129. reason = "禁止频繁单条发送";
  130. System.out.println("禁止频繁单条发送");
  131. }else if(resultCode.equals("111")){
  132. reason = "系统暂定发送";
  133. System.out.println("系统暂定发送");
  134. }else if(resultCode.equals("112")){
  135. reason = "号码错误";
  136. System.out.println("号码错误");
  137. }else if(resultCode.equals("113")){
  138. reason = "定时时间格式不对";
  139. System.out.println("定时时间格式不对");
  140. }else if(resultCode.equals("114")){
  141. reason = "账号被锁,10分钟后登录";
  142. System.out.println("账号被锁,10分钟后登录");
  143. }else if(resultCode.equals("115")){
  144. reason = "连接失败";
  145. System.out.println("连接失败");
  146. }else if(resultCode.equals("116")){
  147. reason = "禁止接口发送";
  148. System.out.println("禁止接口发送");
  149. }else if(resultCode.equals("117")){
  150. reason = "绑定IP不正确";
  151. System.out.println("绑定IP不正确");
  152. }else if(resultCode.equals("120")){
  153. reason = "系统升级";
  154. System.out.println("系统升级");
  155. }else{
  156. reason = "未知错误";
  157. }
  158. return reason;
  159. }
  160. /**
  161. * 将List转换成字符串数组
  162. * @param list
  163. * @return
  164. */
  165. public static String[] listToStringArray(List<String> list){
  166. String str[] = new String[list.size()];
  167. for (int i = 0; i < list.size(); i++) {
  168. str[i] = list.get(i);
  169. }
  170. return str;
  171. }
  172. }

sms.properties

  1. uid=XXX
  2. pwd=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

行业短信接口(提供API改动版)

PropertiesConfig.java

  1. package com.axt.sms.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.FileInputStream;
  4. import java.io.InputStream;
  5. import java.util.Properties;
  6. /**
  7. *
  8. * @author Wentasy
  9. * @createtime 2013年1月13日14:58:54
  10. * @version 1.0
  11. * @since JDK 1.6
  12. */
  13. public class PropertiesConfig {
  14. /**
  15. * 获取整个配置文件中的属性
  16. * @param filePath 文件路径,即文件所在包的路径,例如:java/util/config.properties
  17. */
  18. public static Properties readData(String filePath) {
  19. filePath = getRealPath(filePath);
  20. Properties props = new Properties();
  21. try {
  22. InputStream in = new BufferedInputStream(new FileInputStream(filePath));
  23. props.load(in);
  24. in.close();
  25. return props;
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. return null;
  29. }
  30. }
  31. private static String getRealPath(String filePath) {
  32. //获取绝对路径 并截掉路径的”file:/“前缀
  33. return PropertiesConfig.class.getResource("/" + filePath).toString().substring(6);
  34. }
  35. }

SmsClientAccessTool.java

  1. package com.axt.sms.util;
  2. import java.io.BufferedReader;
  3. import java.io.BufferedWriter;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. /**
  10. * <p>
  11. * <date>2012-03-01</date><br/>
  12. * <span>软维提供的JAVA接口信息(短信,彩信)调用API</span><br/>
  13. * <span>----------基础访问方法-------------</span>
  14. * </p>
  15. *
  16. * @author LIP
  17. * @version 1.0.1
  18. */
  19. public class SmsClientAccessTool {
  20. private static SmsClientAccessTool smsClientToolInstance;
  21. /**
  22. * 采用单列方式来访问操作
  23. *
  24. * @return
  25. */
  26. public static synchronized SmsClientAccessTool getInstance() {
  27. if (smsClientToolInstance == null) {
  28. smsClientToolInstance = new SmsClientAccessTool();
  29. }
  30. return smsClientToolInstance;
  31. }
  32. /**
  33. * <p>
  34. * POST方法
  35. * </p>
  36. *
  37. * @param sendUrl
  38. * :访问URL
  39. * @param paramStr
  40. * :参数串
  41. * @param backEncodType
  42. * :返回的编码
  43. * @return
  44. */
  45. public String doAccessHTTPPost(String sendUrl, String sendParam,
  46. String backEncodType) {
  47. StringBuffer receive = new StringBuffer();
  48. BufferedWriter wr = null;
  49. try {
  50. if (backEncodType == null || backEncodType.equals("")) {
  51. backEncodType = "UTF-8";
  52. }
  53. URL url = new URL(sendUrl);
  54. HttpURLConnection URLConn = (HttpURLConnection) url
  55. .openConnection();
  56. URLConn.setDoOutput(true);
  57. URLConn.setDoInput(true);
  58. ((HttpURLConnection) URLConn).setRequestMethod("POST");
  59. URLConn.setUseCaches(false);
  60. URLConn.setAllowUserInteraction(true);
  61. HttpURLConnection.setFollowRedirects(true);
  62. URLConn.setInstanceFollowRedirects(true);
  63. URLConn.setRequestProperty("Content-Type",
  64. "application/x-www-form-urlencoded;charset=UTF-8");
  65. URLConn.setRequestProperty("Content-Length", String
  66. .valueOf(sendParam.getBytes().length));
  67. DataOutputStream dos = new DataOutputStream(URLConn
  68. .getOutputStream());
  69. dos.writeBytes(sendParam);
  70. BufferedReader rd = new BufferedReader(new InputStreamReader(
  71. URLConn.getInputStream(), backEncodType));
  72. String line;
  73. while ((line = rd.readLine()) != null) {
  74. receive.append(line).append("\r\n");
  75. }
  76. rd.close();
  77. } catch (java.io.IOException e) {
  78. receive.append("访问产生了异常-->").append(e.getMessage());
  79. e.printStackTrace();
  80. } finally {
  81. if (wr != null) {
  82. try {
  83. wr.close();
  84. } catch (IOException ex) {
  85. ex.printStackTrace();
  86. }
  87. wr = null;
  88. }
  89. }
  90. return receive.toString();
  91. }
  92. public String doAccessHTTPGet(String sendUrl, String backEncodType) {
  93. StringBuffer receive = new StringBuffer();
  94. BufferedReader in = null;
  95. try {
  96. if (backEncodType == null || backEncodType.equals("")) {
  97. backEncodType = "UTF-8";
  98. }
  99. URL url = new URL(sendUrl);
  100. HttpURLConnection URLConn = (HttpURLConnection) url
  101. .openConnection();
  102. URLConn.setDoInput(true);
  103. URLConn.setDoOutput(true);
  104. URLConn.connect();
  105. URLConn.getOutputStream().flush();
  106. in = new BufferedReader(new InputStreamReader(URLConn
  107. .getInputStream(), backEncodType));
  108. String line;
  109. while ((line = in.readLine()) != null) {
  110. receive.append(line).append("\r\n");
  111. }
  112. } catch (IOException e) {
  113. receive.append("访问产生了异常-->").append(e.getMessage());
  114. e.printStackTrace();
  115. } finally {
  116. if (in != null) {
  117. try {
  118. in.close();
  119. } catch (java.io.IOException ex) {
  120. ex.printStackTrace();
  121. }
  122. in = null;
  123. }
  124. }
  125. return receive.toString();
  126. }
  127. }

SmsClientSend.java

  1. package com.axt.sms.util;
  2. import java.net.URLEncoder;
  3. /**
  4. * <p>
  5. * <date>2012-03-01</date><br/>
  6. * <span>软维提供的JAVA接口信息(短信,彩信)调用API</span><br/>
  7. * <span>----------发送短信-------------</span>
  8. * </p>
  9. *
  10. * @author LIP
  11. * @version 1.0.1
  12. */
  13. public class SmsClientSend {
  14. /**
  15. * <p>
  16. * <date>2012-03-01</date><br/>
  17. * <span>发送信息方法1--必须传入必填内容</span><br/>
  18. * <p>
  19. * 其一:发送方式,默认为POST<br/>
  20. * 其二:发送内容编码方式,默认为UTF-8
  21. * </p>
  22. * <br/>
  23. * </p>
  24. *
  25. * @param url
  26. * :必填--发送连接地址URL--比如>http://118.145.30.35/sms.aspx
  27. * @param userid
  28. * :必填--用户ID,为数字
  29. * @param account
  30. * :必填--用户帐号
  31. * @param password
  32. * :必填--用户密码
  33. * @param mobile
  34. * :必填--发送的手机号码,多个可以用逗号隔比如>135XXXXXXXX,136XXXXXXXX
  35. * @param content
  36. * :必填--实际发送内容,
  37. * @return 返回发送信息之后返回字符串
  38. */
  39. public static String sendSms(String url, String userid, String account,
  40. String password, String mobile, String content) {
  41. return sendSms(url, userid, account, password, mobile, content, null,
  42. null, null, null, null, null, null, "POST", "UTF-8", "UTF-8");
  43. }
  44. /**
  45. * <p>
  46. * <date>2012-03-01</date><br/>
  47. * <span>发送信息方法--暂时私有化,这里仅仅是提供用户接口而已。其实用不了那么复杂</span><br/>
  48. * <span>发送信息最终的组合形如:http://118.145.30.35/sms.aspx?action=send</span>
  49. * </p>
  50. *
  51. * @param url
  52. * :必填--发送连接地址URL--比如>http://118.145.30.35/sms.aspx
  53. *
  54. * @param userid
  55. * :必填--用户ID,为数字
  56. * @param account
  57. * :必填--用户帐号
  58. * @param password
  59. * :必填--用户密码
  60. * @param mobile
  61. * :必填--发送的手机号码,多个可以用逗号隔比如>135XXXXXXXX,136XXXXXXXX
  62. * @param content
  63. * :必填--实际发送内容,
  64. * @param action
  65. * :选填--访问的事件,默认为send
  66. * @param sendTime
  67. * :选填--定时发送时间,不填则为立即发送,时间格式如>2011-11-11 11:11:11
  68. * @param checkContent
  69. * :选填--检查是否包含非法关键字,1--表示需要检查,0--表示不检查
  70. * @param taskName
  71. * :选填--任务名称,本次任务描述,100字内
  72. * @param countNumber
  73. * :选填--提交号码总数
  74. * @param mobileNumber
  75. * :选填--手机号码总数
  76. * @param telephoneNumber
  77. * :选填--小灵通(和)或座机总数
  78. * @param sendType
  79. * :选填--发送方式,默认为POST
  80. * @param codingType
  81. * :选填--发送内容编码方式,默认为UTF-8
  82. * @param backEncodType
  83. * :选填--返回内容编码方式,默认为UTF-8
  84. * @return 返回发送之后收到的信息
  85. */
  86. private static String sendSms(String url, String userid, String account,
  87. String password, String mobile, String content, String action,
  88. String sendTime, String checkContent, String taskName,
  89. String countNumber, String mobileNumber, String telephoneNumber,
  90. String sendType, String codingType, String backEncodType) {
  91. try {
  92. if (codingType == null || codingType.equals("")) {
  93. codingType = "UTF-8";
  94. }
  95. if (backEncodType == null || backEncodType.equals("")) {
  96. backEncodType = "UTF-8";
  97. }
  98. StringBuffer send = new StringBuffer();
  99. if (action != null && !action.equals("")) {
  100. send.append("action=").append(action);
  101. } else {
  102. send.append("action=send");
  103. }
  104. send.append("&userid=").append(userid);
  105. send.append("&account=").append(
  106. URLEncoder.encode(account, codingType));
  107. send.append("&password=").append(
  108. URLEncoder.encode(password, codingType));
  109. send.append("&mobile=").append(mobile);
  110. send.append("&content=").append(
  111. URLEncoder.encode(content, codingType));
  112. if (sendTime != null && !sendTime.equals("")) {
  113. send.append("&sendTime=").append(
  114. URLEncoder.encode(sendTime, codingType));
  115. }
  116. if (checkContent != null && !checkContent.equals("")) {
  117. send.append("&checkContent=").append(checkContent);
  118. }
  119. if (taskName != null && !taskName.equals("")) {
  120. send.append("&taskName=").append(
  121. URLEncoder.encode(taskName, codingType));
  122. }
  123. if (countNumber != null && !countNumber.equals("")) {
  124. send.append("&countNumber=").append(countNumber);
  125. }
  126. if (mobileNumber != null && !mobileNumber.equals("")) {
  127. send.append("&mobileNumber=").append(mobileNumber);
  128. }
  129. if (telephoneNumber != null && !telephoneNumber.equals("")) {
  130. send.append("&telephoneNumber=").append(telephoneNumber);
  131. }
  132. if (sendType != null && (sendType.toLowerCase()).equals("get")) {
  133. return SmsClientAccessTool.getInstance().doAccessHTTPGet(
  134. url + "?" + send.toString(), backEncodType);
  135. } else {
  136. return SmsClientAccessTool.getInstance().doAccessHTTPPost(url,
  137. send.toString(), backEncodType);
  138. }
  139. } catch (Exception e) {
  140. e.printStackTrace();
  141. return "未发送,编码异常";
  142. }
  143. }
  144. }

SmsUtil.java

  1. package com.axt.sms.util;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Properties;
  5. /**
  6. * @className: SmsUtil.java
  7. * @classDescription:
  8. * @function:
  9. * @author: Wentasy
  10. * @createTime: 2013-3-5 上午09:45:24
  11. * @modifyTime:
  12. * @modifyReason:
  13. * @since: JDK 1.6
  14. */
  15. public class SmsUtil {
  16. /**
  17. *
  18. * @param mobile
  19. * :必填--发送的手机号码,多个可以用逗号隔比如>135XXXXXXXX,136XXXXXXXX
  20. * @param content
  21. * :发送内容 短信内容+公司或单位简称
  22. * @return 返回发送之后收到的信息
  23. */
  24. public static String sendSms(String mobile, String content){
  25. String url = "http://XXX.XXX.XXX.XXX:XXXX/sms.aspx";
  26. Properties props = PropertiesConfig.readData("sms.properties");
  27. String userid = props.getProperty("userid");
  28. String account = props.getProperty("account");
  29. String password = props.getProperty("password");
  30. String result = SmsClientSend.sendSms(url, userid, account, password, mobile, content);
  31. return result;
  32. }
  33. /**
  34. * 将List转换成字符串
  35. * @param list
  36. * @return 转换后的字符串
  37. */
  38. public static String listToString(List<String> list){
  39. String str = "";
  40. int len = list.size();
  41. for (int i = 0; i < len; i++) {
  42. if(i != len - 1){
  43. str += list.get(i) + ",";
  44. }else{
  45. str += list.get(i) + "";
  46. }
  47. }
  48. return str;
  49. }
  50. public static void main(String[] args) {
  51. List<String> list = new ArrayList<String>();
  52. list.add("152XXXXXXXX");
  53. list.add("135XXXXXXXX");
  54. System.out.println(SmsUtil.listToString(list));
  55. }
  56. }

sms.properties

  1. userid=XXX
  2. account=XXXXXXXXXXX
  3. password=XXXXXXXXXXXX










katoon Sina  CSDN
@Wentasy 博文仅供参考,欢迎大家来访。如有错误之处,希望批评指正。原创博文如需转载请注明出处,谢谢 :) [CSDN博客]

发表评论

表情:
评论列表 (有 0 条评论,279人围观)

还没有评论,来说两句吧...

相关阅读

    相关 项目集成总结

            短信接口         项目需要集成短信功能。网上查了很多资料,了解到有下面几种短信途径。           1.      可以通过移动等运营商