|
@@ -0,0 +1,196 @@
|
|
|
+package com.jeeplus.sys.sms;
|
|
|
+
|
|
|
+import com.jeeplus.sys.utils.Global;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+/*
|
|
|
+功能: 企信通PHP HTTP接口 发送短信
|
|
|
+修改日期: 2014-03-19
|
|
|
+说明: http://api.cnsms.cn/?ac=send&uid=用户账号&pwd=MD5位32密码&mobile=号码&content=内容
|
|
|
+状态:
|
|
|
+ 100 发送成功
|
|
|
+ 101 验证失败
|
|
|
+ 102 短信不足
|
|
|
+ 103 操作失败
|
|
|
+ 104 非法字符
|
|
|
+ 105 内容过多
|
|
|
+ 106 号码过多
|
|
|
+ 107 频率过快
|
|
|
+ 108 号码内容空
|
|
|
+ 109 账号冻结
|
|
|
+ 110 禁止频繁单条发送
|
|
|
+ 111 系统暂定发送
|
|
|
+ 112 号码不正确
|
|
|
+ 120 系统升级
|
|
|
+*/
|
|
|
+public class SMSUtils {
|
|
|
+ //发送短信,uid,pwd,参数值请向企信通申请, tel:发送的手机号, content:发送的内容
|
|
|
+ public static String send(String mobile,String content,String sendTime,String checkcontent) throws IOException {
|
|
|
+ BufferedReader in = null;
|
|
|
+ String result = "";
|
|
|
+ try {
|
|
|
+ // 创建StringBuffer对象用来操作字符串
|
|
|
+ StringBuffer sb = new StringBuffer("http://www.lcqxt.com/sms.aspx?action=send");
|
|
|
+
|
|
|
+ // 向StringBuffer追加用户名
|
|
|
+ sb.append("&userid=" + Global.getSmsUserid());//在此申请企信通uid,并进行配置用户名
|
|
|
+
|
|
|
+ // 向StringBuffer追加密码(密码采用MD5 32位 小写)
|
|
|
+ sb.append("&account=" + Global.getSmsAccount());
|
|
|
+
|
|
|
+ // 向StringBuffer追加密码(密码采用MD5 32位 小写)
|
|
|
+ //sb.append("&pwd="+Digests.string2MD5(pwd));//在此申请企信通uid,并进行配置密码
|
|
|
+ sb.append("&password=" + Global.getSmsPassword());
|
|
|
+ // 向StringBuffer追加手机号码
|
|
|
+ sb.append("&mobile=" + mobile);
|
|
|
+ // 向StringBuffer追加消息内容转URL标准码
|
|
|
+ sb.append("&content=" + URLEncoder.encode(content, "utf8"));
|
|
|
+ sb.append("&sendTime=" + sendTime);
|
|
|
+ sb.append("&checkcontent=" + checkcontent);
|
|
|
+
|
|
|
+ // 创建url对象
|
|
|
+ URL url = new URL(sb.toString());
|
|
|
+
|
|
|
+ // 打开url连接
|
|
|
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ // 设置url请求方式 ‘get’ 或者 ‘post’
|
|
|
+ connection.setRequestMethod("POST");
|
|
|
+
|
|
|
+ // 发送
|
|
|
+ in = new BufferedReader(new InputStreamReader(url.openStream()));
|
|
|
+
|
|
|
+ // 返回发送结果
|
|
|
+ String line;
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ result += line;
|
|
|
+ }
|
|
|
+ }catch (Exception e) {
|
|
|
+ System.out.println("发送 POST 请求出现异常!"+e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ //使用finally块来关闭输出流、输入流
|
|
|
+ finally{
|
|
|
+ try{
|
|
|
+ if(in!=null){
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch(IOException ex){
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+// //容联云通讯(160040--超过同模板同号码上限)
|
|
|
+// public static String sends(String mobile,String randomCode) throws IOException {
|
|
|
+// BufferedReader reader = null;
|
|
|
+// StringBuffer result = new StringBuffer("");
|
|
|
+// try {
|
|
|
+// StringBuffer sb = new StringBuffer("https://app.cloopen.com:8883/2013-12-26/Accounts/"+Global.getRongUserid()+"/SMS/TemplateSMS?");
|
|
|
+// StringBuffer sbr = new StringBuffer();
|
|
|
+// String aid = Global.getRongUserid();
|
|
|
+// String atoken = Global.getRongToken();
|
|
|
+// SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmss" );
|
|
|
+// String time =sdf.format(new Date());
|
|
|
+// sbr.append(aid).append(atoken).append(time);
|
|
|
+// //模板指定参数
|
|
|
+// String [] s = new String[]{randomCode,"60"};
|
|
|
+// //sig
|
|
|
+// sb.append("sig="+ MD5.toMD5(sbr.toString()));
|
|
|
+//
|
|
|
+// // 创建url对象
|
|
|
+// URL url = new URL(sb.toString());
|
|
|
+//
|
|
|
+// // 打开url连接
|
|
|
+// HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
+//
|
|
|
+// // 设置url请求方式 ‘get’ 或者 ‘post’
|
|
|
+// connection.setRequestMethod("POST");
|
|
|
+// connection.setRequestProperty("Accept","application/json");
|
|
|
+// connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
|
+// String an = Global.getRongUserid()+":"+time;
|
|
|
+// BASE64Encoder encoder = new BASE64Encoder();
|
|
|
+// an = encoder.encode(an.getBytes("UTF-8"));
|
|
|
+// connection.setRequestProperty("Authorization", an);
|
|
|
+// connection.setRequestProperty("Content-Length", "256");
|
|
|
+// connection.setDoOutput(true);
|
|
|
+// connection.connect();
|
|
|
+// //post请求
|
|
|
+// DataOutputStream out = new DataOutputStream(connection.getOutputStream());
|
|
|
+// JSONObject obj = new JSONObject();
|
|
|
+// obj.element("to", mobile);
|
|
|
+// obj.element("appId", Global.getAppId());
|
|
|
+// obj.element("templateId", Global.getTemplateId());
|
|
|
+// obj.element("datas", s);
|
|
|
+//
|
|
|
+// out.writeBytes(obj.toString());
|
|
|
+// out.flush();
|
|
|
+// out.close();
|
|
|
+// // 发送
|
|
|
+// reader = new BufferedReader(new InputStreamReader(
|
|
|
+// connection.getInputStream()));
|
|
|
+// String lines;
|
|
|
+// while ((lines = reader.readLine()) != null) {
|
|
|
+// lines = new String(lines.getBytes(), "utf-8");
|
|
|
+// result.append(lines);
|
|
|
+// }
|
|
|
+// System.out.println(result);
|
|
|
+// // 断开连接
|
|
|
+// connection.disconnect();
|
|
|
+// }catch (Exception e) {
|
|
|
+// System.out.println("发送 POST 请求出现异常!"+e);
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// //使用finally块来关闭输出流、输入流
|
|
|
+// finally{
|
|
|
+// try{
|
|
|
+// if(reader!=null){
|
|
|
+// reader.close();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// catch(IOException ex){
|
|
|
+// ex.printStackTrace();
|
|
|
+// }
|
|
|
+// return result.toString();
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+ //容联云通讯(160040--超过同模板同号码上限)
|
|
|
+ public static HashMap<String,Object> sends(String mobile,String randomCode) throws IOException {
|
|
|
+ HashMap<String, Object> result = null;
|
|
|
+ CCPRestSDK restAPI = new CCPRestSDK();
|
|
|
+ try {
|
|
|
+ String aid = Global.getRongUserid();
|
|
|
+ String atoken = Global.getRongToken();
|
|
|
+ String appId = Global.getAppId();
|
|
|
+ String templateId = Global.getTemplateId();
|
|
|
+ restAPI.init("app.cloopen.com", "8883");
|
|
|
+ restAPI.setAccount(aid, atoken);
|
|
|
+ restAPI.setAppId(appId);
|
|
|
+ result = restAPI.sendTemplateSMS(mobile,templateId,new String[]{randomCode,"60"});
|
|
|
+ }catch (Exception e) {
|
|
|
+ System.out.println("发送 POST 请求出现异常!"+e);
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+// public static void main(String[] args) throws Exception {
|
|
|
+// String inputline =sends("15201428039","系统预警");
|
|
|
+// System.out.println(inputline);
|
|
|
+// long sys = System.currentTimeMillis();
|
|
|
+// System.out.println(sys);
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|