2023年7月31日发(作者:)
发送短信功能-联通接⼝配置⽂件:url=:9600/sms/Api/earchNumber=:9600/sms/Api/eply=:9600/sms/Api/de=*****username=******password=******
正⽂内容:package ;import ption;import tream;import imal;import DateFormat;import ;import ties;import r;import n;import ient;import thod;import thodParams;//:9600/sms/Api/?SpCode=000001&LoginName=admin&Password=admin&MessageContent=你有⼀项编号为123456789的事务需要处理。&UserNumber=186********&SerialNumber=&ScheduleTime=&f=1
public class SMSSenderUnicom { //发送参数 private String spcode = "*******";//⽤户的企业编码 private String user_name = "******"; //⽤户名 private String password = "******"; //密码 private String url = ":9600/sms/Api/"; private String filePath = "/ties"; // >>> 发送⽅法 ############# //传⼊:id:DB中短信唯⼀ID;mobile:⼿机号码;message:短信内容 public String SendSms(String id, String mobile, String message) throws IOException { //从配置⽂件获取参数,已设置默认值 url = getResource(filePath,"url"); spcode = getResource(filePath,"spcode"); user_name = getResource(filePath,"username"); password = getResource(filePath,"password");
n("发送短信:"+url+","+spcode+","+user_name+","+password);
//⼿机号码处理:删除⽆效字符,替换⼿机号码中的86,086 try { mobile = checkPhoneNum(mobile); } catch (Exception e1) { tackTrace(); } n("格式后的电话号码:"+mobile); if("".equals(mobile)){ return "result=-4&description=⼿机号码为空"; }
//替换掉短信内容中的换⾏和空格 message = eAll(" ", "").replaceAll("rn", ""); message = eAll("*******", "").replaceAll("*******", ""); message = new String(es(),"GBK"); n("格式后的短信为:"+message);
String info = ""; try{ HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(url);// ams().setParameter(_CONTENT_CHARSET,"gbk"); ameter("SpCode", spcode); ameter("LoginName", user_name); ameter("Password", password); ameter("MessageContent", message); ameter("UserNumber", mobile); ameter("SerialNumber", ""); //getCurrentTimeAsNumber(id) ameter("ScheduleTime", ""); ameter("ExtendAccessNum", ""); ameter("f", "1"); eMethod(post); info = new String(ponseBody(),"gbk"); n(("HttpClient 发送信息,返回值[%s]", info)); }catch (Exception e) { tackTrace(); }
//成功时,返回值:result=0&description=发送短信成功&taskid=237108123977&faillist=&task_id=237108123977 //失败时,返回值:result=28&description=发送内容与模板不符 return info; } //从配置⽂件获取参数⽅法 public String getResource(String filePath,String key) throws IOException { public String getResource(String filePath,String key) throws IOException { Properties props = new Properties(); //返回读取指定资源的输⼊流 try { InputStream in = ss().getResourceAsStream(filePath);
(in); String value = perty(key).toString().trim(); return value; } catch (Exception e) { tackTrace(); return null; } } //规范电话号码:去掉86,086 protected String checkPhoneNum(String phoneNum) throws Exception { //替换⼿机号码中的⽆效字符 phoneNum = eAll(" ","").replaceAll("-","").replaceAll("+","");
Pattern p1 = e("^((0{0,1}86){0,1})1[0-9]{10}"); Matcher m1 = r(phoneNum); if (s()) { Pattern p2 = e("^((0{0,1}86){0,1})"); Matcher m2 = r(phoneNum); StringBuffer sb = new StringBuffer(); while (()) { Replacement(sb, ""); } Tail(sb); return ng(); } else { throw new Exception("电话号码"+phoneNum+" 格式错误!"); } }
/** * 返回当前⽇期时间字符串
* 默认格式:yyyymmddhhmmssSSS */ public static BigDecimal getCurrentTimeAsNumber(String id) { String returnStr = null; SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmssSSS"); Date date = new Date(); returnStr = ((date)+"000000").substring(0,()) + id; return new BigDecimal(returnStr); }
//获取剩余短信条数 public String smsRemain() throws IOException { //从配置⽂件获取参数,已设置默认值 url = getResource(filePath,"urlSearchNumber"); spcode = getResource(filePath,"spcode"); user_name = getResource(filePath,"username"); password = getResource(filePath,"password");
n("获取短信条数:"+url+","+spcode+","+user_name+","+password); n("获取短信条数:"+url+","+spcode+","+user_name+","+password);
String info = ""; try{ HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(url);// ams().setParameter(_CONTENT_CHARSET,"gbk"); ameter("SpCode", spcode); ameter("LoginName", user_name); ameter("Password", password); eMethod(post); info = new String(ponseBody(),"gbk"); n(("HttpClient 发送信息,返回值[%s]", info)); }catch (Exception e) { tackTrace(); }finally{
} return info; }
//截取返回值 public String getQueryString(String sMain,String sSub) throws IOException { String sTemp = ""; int iPos = f(sSub+"="); if (iPos>=0){ sTemp = ing(iPos+()+1); if(f("&")>=0){ sTemp = ing(0,f("&")); } } return sTemp; } //测试 public static void main(String[] args) throws IOException { SMSSenderUnicom test = new SMSSenderUnicom(); //测试短信发送,并返回值 String res = s("1234567890","+086-182-****-****", "温馨提醒:按L12A083602合同约定,请05⽉12⽇前将当期租⾦¥9,445.03元⾜额汇给我司,以免产⽣罚息,谢谢"); n(res); //测试获取剩余短信条数,并返回值 String ress= ain(); n(ress); }}
2023年7月31日发(作者:)
发送短信功能-联通接⼝配置⽂件:url=:9600/sms/Api/earchNumber=:9600/sms/Api/eply=:9600/sms/Api/de=*****username=******password=******
正⽂内容:package ;import ption;import tream;import imal;import DateFormat;import ;import ties;import r;import n;import ient;import thod;import thodParams;//:9600/sms/Api/?SpCode=000001&LoginName=admin&Password=admin&MessageContent=你有⼀项编号为123456789的事务需要处理。&UserNumber=186********&SerialNumber=&ScheduleTime=&f=1
public class SMSSenderUnicom { //发送参数 private String spcode = "*******";//⽤户的企业编码 private String user_name = "******"; //⽤户名 private String password = "******"; //密码 private String url = ":9600/sms/Api/"; private String filePath = "/ties"; // >>> 发送⽅法 ############# //传⼊:id:DB中短信唯⼀ID;mobile:⼿机号码;message:短信内容 public String SendSms(String id, String mobile, String message) throws IOException { //从配置⽂件获取参数,已设置默认值 url = getResource(filePath,"url"); spcode = getResource(filePath,"spcode"); user_name = getResource(filePath,"username"); password = getResource(filePath,"password");
n("发送短信:"+url+","+spcode+","+user_name+","+password);
//⼿机号码处理:删除⽆效字符,替换⼿机号码中的86,086 try { mobile = checkPhoneNum(mobile); } catch (Exception e1) { tackTrace(); } n("格式后的电话号码:"+mobile); if("".equals(mobile)){ return "result=-4&description=⼿机号码为空"; }
//替换掉短信内容中的换⾏和空格 message = eAll(" ", "").replaceAll("rn", ""); message = eAll("*******", "").replaceAll("*******", ""); message = new String(es(),"GBK"); n("格式后的短信为:"+message);
String info = ""; try{ HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(url);// ams().setParameter(_CONTENT_CHARSET,"gbk"); ameter("SpCode", spcode); ameter("LoginName", user_name); ameter("Password", password); ameter("MessageContent", message); ameter("UserNumber", mobile); ameter("SerialNumber", ""); //getCurrentTimeAsNumber(id) ameter("ScheduleTime", ""); ameter("ExtendAccessNum", ""); ameter("f", "1"); eMethod(post); info = new String(ponseBody(),"gbk"); n(("HttpClient 发送信息,返回值[%s]", info)); }catch (Exception e) { tackTrace(); }
//成功时,返回值:result=0&description=发送短信成功&taskid=237108123977&faillist=&task_id=237108123977 //失败时,返回值:result=28&description=发送内容与模板不符 return info; } //从配置⽂件获取参数⽅法 public String getResource(String filePath,String key) throws IOException { public String getResource(String filePath,String key) throws IOException { Properties props = new Properties(); //返回读取指定资源的输⼊流 try { InputStream in = ss().getResourceAsStream(filePath);
(in); String value = perty(key).toString().trim(); return value; } catch (Exception e) { tackTrace(); return null; } } //规范电话号码:去掉86,086 protected String checkPhoneNum(String phoneNum) throws Exception { //替换⼿机号码中的⽆效字符 phoneNum = eAll(" ","").replaceAll("-","").replaceAll("+","");
Pattern p1 = e("^((0{0,1}86){0,1})1[0-9]{10}"); Matcher m1 = r(phoneNum); if (s()) { Pattern p2 = e("^((0{0,1}86){0,1})"); Matcher m2 = r(phoneNum); StringBuffer sb = new StringBuffer(); while (()) { Replacement(sb, ""); } Tail(sb); return ng(); } else { throw new Exception("电话号码"+phoneNum+" 格式错误!"); } }
/** * 返回当前⽇期时间字符串
* 默认格式:yyyymmddhhmmssSSS */ public static BigDecimal getCurrentTimeAsNumber(String id) { String returnStr = null; SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmssSSS"); Date date = new Date(); returnStr = ((date)+"000000").substring(0,()) + id; return new BigDecimal(returnStr); }
//获取剩余短信条数 public String smsRemain() throws IOException { //从配置⽂件获取参数,已设置默认值 url = getResource(filePath,"urlSearchNumber"); spcode = getResource(filePath,"spcode"); user_name = getResource(filePath,"username"); password = getResource(filePath,"password");
n("获取短信条数:"+url+","+spcode+","+user_name+","+password); n("获取短信条数:"+url+","+spcode+","+user_name+","+password);
String info = ""; try{ HttpClient httpclient = new HttpClient(); PostMethod post = new PostMethod(url);// ams().setParameter(_CONTENT_CHARSET,"gbk"); ameter("SpCode", spcode); ameter("LoginName", user_name); ameter("Password", password); eMethod(post); info = new String(ponseBody(),"gbk"); n(("HttpClient 发送信息,返回值[%s]", info)); }catch (Exception e) { tackTrace(); }finally{
} return info; }
//截取返回值 public String getQueryString(String sMain,String sSub) throws IOException { String sTemp = ""; int iPos = f(sSub+"="); if (iPos>=0){ sTemp = ing(iPos+()+1); if(f("&")>=0){ sTemp = ing(0,f("&")); } } return sTemp; } //测试 public static void main(String[] args) throws IOException { SMSSenderUnicom test = new SMSSenderUnicom(); //测试短信发送,并返回值 String res = s("1234567890","+086-182-****-****", "温馨提醒:按L12A083602合同约定,请05⽉12⽇前将当期租⾦¥9,445.03元⾜额汇给我司,以免产⽣罚息,谢谢"); n(res); //测试获取剩余短信条数,并返回值 String ress= ain(); n(ress); }}
发布评论