| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package com.cnd3b.utility;
- import com.alibaba.fastjson.JSONObject;
- import com.cnd3b.common.BaseClass;
- import p2.pao.PaoRemote;
- 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.Calendar;
- import java.util.HashMap;
- public class Sms extends BaseClass {
- public static void main(String[] args) {
- Sms sms = new Sms();
- // sms.sendout("13732579910", "12345");
- System.err.println(sms.queryBalance());
- }
- public void sendOutMsg(String phone, String msg) {
- msg = "【布万家】您好,您的验证码是" + msg;
- sendout(msg, phone);
- }
- private void sendout(String msg, String phone) {
- //短信下发
- String sendUrl = " http://smssh1.253.com/msg/send/json";
- HashMap<String, String> map = new HashMap();
- map.put("account", "YZM2628187");//API账号
- map.put("password", "I8ylSr7kc");//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<String, String> 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;
- }
- }
|