123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- package com.jeeplus.common.sms;
- import java.io.*;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- import com.jeeplus.common.config.Global;
- import com.jeeplus.common.security.Digests;
- import com.jeeplus.modules.esignature.utils.MD5;
- import net.sf.json.JSONObject;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import sun.misc.BASE64Encoder;
- /*
- 功能: 企信通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,"10"};
- //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();
- }
- }
- public static void main(String[] args) throws Exception {
- String inputline =sends("15201428039","系统预警");
- System.out.println(inputline);
- long sys = System.currentTimeMillis();
- System.out.println(sys);
- }
- }
|