Sms2.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.cnd3b.utility;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.cnd3b.common.BaseClass;
  4. import p2.pao.PaoSetRemote;
  5. import java.io.BufferedReader;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStream;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.nio.charset.StandardCharsets;
  11. import java.util.HashMap;
  12. public class Sms2 extends BaseClass {
  13. public static void main(String[] args) {
  14. Sms2 sms = new Sms2();
  15. sms.sendOutMsg("13732579910","123112" );
  16. // System.err.println(sms.queryBalance());
  17. }
  18. public void sendOutMsg(String phone, String msg) {
  19. msg = "【Banninger】工单号:12312313已指派请处理,客户名称:张三,提交人:sjw,备注:备注信息";
  20. sendout(msg, phone);
  21. }
  22. private void sendout(String msg, String phone) {
  23. //短信下发
  24. String sendUrl = " http://smssh1.253.com/msg/v1/send/json";
  25. HashMap<String, String> map = new HashMap();
  26. map.put("account", "N3147701");//API账号
  27. map.put("password", "4ClGyab6fv6a87");//API密码
  28. map.put("msg", msg);//短信内容
  29. map.put("phone", phone);//手机号
  30. map.put("report", "true");//是否需要状态报告
  31. map.put("extend", "");//自定义扩展码
  32. JSONObject js = (JSONObject) JSONObject.toJSON(map);
  33. String result = sendSmsByPost(sendUrl, js.toString());
  34. PaoSetRemote tsmslogSet = null;
  35. try {
  36. JSONObject json = JSONObject.parseObject(result);
  37. String code = json.getString("code");
  38. String msgid = json.getString("msgId");
  39. String time = json.getString("time");
  40. String errorMsg = json.getString("errorMsg");
  41. // tsmslogSet = getP2ServerSystemPaoSet("tsmslog");
  42. // PaoRemote pao = tsmslogSet.addAtEnd();
  43. // pao.setValue("createdate", Calendar.getInstance().getTime(), 11L);
  44. // pao.setValue("fmsg", msg, 11L);
  45. // pao.setValue("fphonenumber", phone, 11L);
  46. // pao.setValue("code", code, 11L);
  47. // pao.setValue("msgid", msgid, 11L);
  48. // pao.setValue("time", time, 11L);
  49. // pao.setValue("errorMsg", errorMsg, 11L);
  50. //
  51. // tsmslogSet.save();
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. } finally {
  55. try {
  56. //tsmslogSet.close();
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. }
  62. public String queryBalance() {
  63. //查询余额
  64. String balanceUrl = "https://smssh1.253.com/msg/balance/json";
  65. HashMap<String, String> map1 = new HashMap();
  66. map1.put("account", "YZM2628187");
  67. map1.put("password", "I8ylSr7kc");
  68. JSONObject js1 = (JSONObject) JSONObject.toJSON(map1);
  69. return sendSmsByPost(balanceUrl, js1.toString());
  70. }
  71. public static String sendSmsByPost(String path, String postContent) {
  72. URL url = null;
  73. try {
  74. url = new URL(path);
  75. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  76. httpURLConnection.setRequestMethod("POST");
  77. httpURLConnection.setConnectTimeout(10000);
  78. httpURLConnection.setReadTimeout(10000);
  79. httpURLConnection.setDoOutput(true);
  80. httpURLConnection.setDoInput(true);
  81. httpURLConnection.setRequestProperty("Charset", "UTF-8");
  82. httpURLConnection.setRequestProperty("Content-Type", "application/json");
  83. httpURLConnection.connect();
  84. OutputStream os = httpURLConnection.getOutputStream();
  85. os.write(postContent.getBytes(StandardCharsets.UTF_8));
  86. os.flush();
  87. StringBuilder sb = new StringBuilder();
  88. int httpRspCode = httpURLConnection.getResponseCode();
  89. if (httpRspCode == HttpURLConnection.HTTP_OK) {
  90. BufferedReader br = new BufferedReader(
  91. new InputStreamReader(httpURLConnection.getInputStream(), StandardCharsets.UTF_8));
  92. String line = null;
  93. while ((line = br.readLine()) != null) {
  94. sb.append(line);
  95. }
  96. br.close();
  97. return sb.toString();
  98. }
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. return null;
  103. }
  104. }