package com.cnd3b.utility; import com.alibaba.fastjson.JSONObject; import com.cnd3b.common.BaseClass; import p2.pao.PaoSetRemote; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.HashMap; public class Sms2 extends BaseClass { public static void main(String[] args) { Sms2 sms = new Sms2(); sms.sendOutMsg("13732579910","123112" ); // System.err.println(sms.queryBalance()); } public void sendOutMsg(String phone, String msg) { msg = "【Banninger】工单号:12312313已指派请处理,客户名称:张三,提交人:sjw,备注:备注信息"; sendout(msg, phone); } private void sendout(String msg, String phone) { //短信下发 String sendUrl = " http://smssh1.253.com/msg/v1/send/json"; HashMap map = new HashMap(); map.put("account", "N3147701");//API账号 map.put("password", "4ClGyab6fv6a87");//API密码 map.put("msg", msg);//短信内容 map.put("phone", phone);//手机号 map.put("report", "true");//是否需要状态报告 map.put("extend", "");//自定义扩展码 JSONObject js = (JSONObject) JSONObject.toJSON(map); String result = sendSmsByPost(sendUrl, js.toString()); PaoSetRemote tsmslogSet = null; try { JSONObject json = JSONObject.parseObject(result); String code = json.getString("code"); String msgid = json.getString("msgId"); String time = json.getString("time"); String errorMsg = json.getString("errorMsg"); // tsmslogSet = getP2ServerSystemPaoSet("tsmslog"); // PaoRemote pao = tsmslogSet.addAtEnd(); // pao.setValue("createdate", Calendar.getInstance().getTime(), 11L); // pao.setValue("fmsg", msg, 11L); // pao.setValue("fphonenumber", phone, 11L); // pao.setValue("code", code, 11L); // pao.setValue("msgid", msgid, 11L); // pao.setValue("time", time, 11L); // pao.setValue("errorMsg", errorMsg, 11L); // // tsmslogSet.save(); } catch (Exception e) { e.printStackTrace(); } finally { try { //tsmslogSet.close(); } catch (Exception e) { e.printStackTrace(); } } } public String queryBalance() { //查询余额 String balanceUrl = "https://smssh1.253.com/msg/balance/json"; HashMap map1 = new HashMap(); map1.put("account", "YZM2628187"); map1.put("password", "I8ylSr7kc"); JSONObject js1 = (JSONObject) JSONObject.toJSON(map1); return sendSmsByPost(balanceUrl, js1.toString()); } public static String sendSmsByPost(String path, String postContent) { URL url = null; try { url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(10000); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); httpURLConnection.setRequestProperty("Charset", "UTF-8"); httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.connect(); OutputStream os = httpURLConnection.getOutputStream(); os.write(postContent.getBytes(StandardCharsets.UTF_8)); os.flush(); StringBuilder sb = new StringBuilder(); int httpRspCode = httpURLConnection.getResponseCode(); if (httpRspCode == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader( new InputStreamReader(httpURLConnection.getInputStream(), StandardCharsets.UTF_8)); String line = null; while ((line = br.readLine()) != null) { sb.append(line); } br.close(); return sb.toString(); } } catch (Exception e) { e.printStackTrace(); } return null; } }