Sms.java 4.3 KB

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