SMSUtils.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.jeeplus.common.sms;
  2. import java.io.*;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import java.net.URLEncoder;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.net.URL;
  10. import java.net.URLConnection;
  11. import java.text.SimpleDateFormat;
  12. import java.util.Date;
  13. import java.util.List;
  14. import java.util.Map;
  15. import com.jeeplus.common.config.Global;
  16. import com.jeeplus.common.security.Digests;
  17. import com.jeeplus.modules.esignature.utils.MD5;
  18. import net.sf.json.JSONObject;
  19. import org.apache.http.HttpEntity;
  20. import org.apache.http.HttpResponse;
  21. import org.apache.http.HttpStatus;
  22. import org.apache.http.client.methods.HttpPost;
  23. import org.apache.http.entity.StringEntity;
  24. import org.apache.http.impl.client.DefaultHttpClient;
  25. import org.apache.http.util.EntityUtils;
  26. import sun.misc.BASE64Encoder;
  27. /*
  28. 功能: 企信通PHP HTTP接口 发送短信
  29. 修改日期: 2014-03-19
  30. 说明: http://api.cnsms.cn/?ac=send&uid=用户账号&pwd=MD5位32密码&mobile=号码&content=内容
  31. 状态:
  32. 100 发送成功
  33. 101 验证失败
  34. 102 短信不足
  35. 103 操作失败
  36. 104 非法字符
  37. 105 内容过多
  38. 106 号码过多
  39. 107 频率过快
  40. 108 号码内容空
  41. 109 账号冻结
  42. 110 禁止频繁单条发送
  43. 111 系统暂定发送
  44. 112 号码不正确
  45. 120 系统升级
  46. */
  47. public class SMSUtils {
  48. //发送短信,uid,pwd,参数值请向企信通申请, tel:发送的手机号, content:发送的内容
  49. public static String send(String mobile,String content,String sendTime,String checkcontent) throws IOException {
  50. BufferedReader in = null;
  51. String result = "";
  52. try {
  53. // 创建StringBuffer对象用来操作字符串
  54. StringBuffer sb = new StringBuffer("http://www.lcqxt.com/sms.aspx?action=send");
  55. // 向StringBuffer追加用户名
  56. sb.append("&userid=" + Global.getSmsUserid());//在此申请企信通uid,并进行配置用户名
  57. // 向StringBuffer追加密码(密码采用MD5 32位 小写)
  58. sb.append("&account=" + Global.getSmsAccount());
  59. // 向StringBuffer追加密码(密码采用MD5 32位 小写)
  60. //sb.append("&pwd="+Digests.string2MD5(pwd));//在此申请企信通uid,并进行配置密码
  61. sb.append("&password=" + Global.getSmsPassword());
  62. // 向StringBuffer追加手机号码
  63. sb.append("&mobile=" + mobile);
  64. // 向StringBuffer追加消息内容转URL标准码
  65. sb.append("&content=" + URLEncoder.encode(content, "utf8"));
  66. sb.append("&sendTime=" + sendTime);
  67. sb.append("&checkcontent=" + checkcontent);
  68. // 创建url对象
  69. URL url = new URL(sb.toString());
  70. // 打开url连接
  71. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  72. // 设置url请求方式 ‘get’ 或者 ‘post’
  73. connection.setRequestMethod("POST");
  74. // 发送
  75. in = new BufferedReader(new InputStreamReader(url.openStream()));
  76. // 返回发送结果
  77. String line;
  78. while ((line = in.readLine()) != null) {
  79. result += line;
  80. }
  81. }catch (Exception e) {
  82. System.out.println("发送 POST 请求出现异常!"+e);
  83. e.printStackTrace();
  84. }
  85. //使用finally块来关闭输出流、输入流
  86. finally{
  87. try{
  88. if(in!=null){
  89. in.close();
  90. }
  91. }
  92. catch(IOException ex){
  93. ex.printStackTrace();
  94. }
  95. return result;
  96. }
  97. }
  98. //容联云通讯(160040--超过同模板同号码上限)
  99. public static String sends(String mobile,String randomCode) throws IOException {
  100. BufferedReader reader = null;
  101. StringBuffer result = new StringBuffer("");
  102. try {
  103. StringBuffer sb = new StringBuffer("https://app.cloopen.com:8883/2013-12-26/Accounts/"+Global.getRongUserid()+"/SMS/TemplateSMS?");
  104. StringBuffer sbr = new StringBuffer();
  105. String aid = Global.getRongUserid();
  106. String atoken = Global.getRongToken();
  107. SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss" );
  108. String time =sdf.format(new Date());
  109. sbr.append(aid).append(atoken).append(time);
  110. //模板指定参数
  111. String [] s = new String[]{randomCode,"10"};
  112. //sig
  113. sb.append("sig="+ MD5.toMD5(sbr.toString()));
  114. // 创建url对象
  115. URL url = new URL(sb.toString());
  116. // 打开url连接
  117. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  118. // 设置url请求方式 ‘get’ 或者 ‘post’
  119. connection.setRequestMethod("POST");
  120. connection.setRequestProperty("Accept","application/json");
  121. connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  122. String an = Global.getRongUserid()+":"+time;
  123. BASE64Encoder encoder = new BASE64Encoder();
  124. an = encoder.encode(an.getBytes("UTF-8"));
  125. connection.setRequestProperty("Authorization", an);
  126. connection.setRequestProperty("Content-Length", "256");
  127. connection.setDoOutput(true);
  128. connection.connect();
  129. //post请求
  130. DataOutputStream out = new DataOutputStream(connection.getOutputStream());
  131. JSONObject obj = new JSONObject();
  132. obj.element("to", mobile);
  133. obj.element("appId", Global.getAppId());
  134. obj.element("templateId", Global.getTemplateId());
  135. obj.element("datas", s);
  136. out.writeBytes(obj.toString());
  137. out.flush();
  138. out.close();
  139. // 发送
  140. reader = new BufferedReader(new InputStreamReader(
  141. connection.getInputStream()));
  142. String lines;
  143. while ((lines = reader.readLine()) != null) {
  144. lines = new String(lines.getBytes(), "utf-8");
  145. result.append(lines);
  146. }
  147. System.out.println(result);
  148. // 断开连接
  149. connection.disconnect();
  150. }catch (Exception e) {
  151. System.out.println("发送 POST 请求出现异常!"+e);
  152. e.printStackTrace();
  153. }
  154. //使用finally块来关闭输出流、输入流
  155. finally{
  156. try{
  157. if(reader!=null){
  158. reader.close();
  159. }
  160. }
  161. catch(IOException ex){
  162. ex.printStackTrace();
  163. }
  164. return result.toString();
  165. }
  166. }
  167. public static void main(String[] args) throws Exception {
  168. String inputline =sends("15201428039","系统预警");
  169. System.out.println(inputline);
  170. long sys = System.currentTimeMillis();
  171. System.out.println(sys);
  172. }
  173. }