package com.product.text.message.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Calendar; import org.apache.commons.codec.binary.Base64; import com.alibaba.fastjson.JSON; import com.product.core.entity.FieldSetEntity; import com.product.text.message.config.CmnConst; import com.product.text.message.entity.SendReq; import com.product.text.message.entity.SendRes; /** * 云MAS服务Http发送 * Copyright PRODUCT-BASE * @Title: PRODUCT-BASE- * @Project: HttpSmsSendUtil * @Date: 2021年6月25日 下午5:09:59 * @Author: 杜洪波 * @Description: */ public class HttpSmsSendUtil { private static String apId = "zgctjt"; //用户 private static String secretKey = "zgctjt2019"; //密码 private static String ecName = "自贡市城市建设投资开发集团有限公司"; // 集团名称 private static String sign = "9OOWx4mtn"; // 网关签名编码 private static String addSerial = ""; // 拓展码 填空 public static String url = "http://112.35.1.155:1992/sms/norsubmit";// 请求url public static void sendMsg(FieldSetEntity fseMessageInfo) { try { SendReq sendReq = new SendReq(); sendReq.setApId(fseMessageInfo.getString(CmnConst.USER_ACCOUNT)); sendReq.setEcName(fseMessageInfo.getString(CmnConst.ECNAME)); sendReq.setSecretKey(fseMessageInfo.getString(CmnConst.SECRETKEY)); sendReq.setContent(fseMessageInfo.getString("content")); sendReq.setMobiles(fseMessageInfo.getString(CmnConst.RECEIVE_USER)); sendReq.setAddSerial(fseMessageInfo.getString(CmnConst.ADDSERIAL)); sendReq.setSign(fseMessageInfo.getString(CmnConst.SIGN)); StringBuilder stringBuffer = new StringBuilder(); stringBuffer.append(sendReq.getEcName()); stringBuffer.append(sendReq.getApId()); stringBuffer.append(sendReq.getSecretKey()); stringBuffer.append(sendReq.getMobiles()); stringBuffer.append(sendReq.getContent()); stringBuffer.append(sendReq.getSign()); stringBuffer.append(sendReq.getAddSerial()); sendReq.setMac(Md5Util.MD5(stringBuffer.toString()).toLowerCase()); String reqText = JSON.toJSONString(sendReq); String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8")); String resStr = sendPost(url, encode); SendRes sendRes = JSON.parseObject(resStr, SendRes.class); if (sendRes.isSuccess()) { fseMessageInfo.setValue(CmnConst.SEND_STAT, 0); } else { fseMessageInfo.setValue(CmnConst.SEND_STAT, 1); fseMessageInfo.setValue(CmnConst.FAILURE_REASON, sendRes.getMsgGroup()); } } catch (IOException e) { fseMessageInfo.setValue(CmnConst.SEND_STAT, 1); fseMessageInfo.setValue(CmnConst.FAILURE_REASON, e.getMessage()); } } /** * 多用户发送短信信息 * * @param mobiles 手机号码逗号分隔 * @param content 短信内容 * @return 返回1表示成功,0表示失败 * @throws IOException */ public static int sendMsg(String mobiles, String content) throws IOException { Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowDatestr = sdf.format(calendar.getTimeInMillis()); //发送日期 content += nowDatestr; // 短信内容后跟个日期时间(可有可无),需求要求 SendReq sendReq = new SendReq(); sendReq.setApId(apId); sendReq.setEcName(ecName); sendReq.setSecretKey(secretKey); sendReq.setContent(content); sendReq.setMobiles(mobiles); sendReq.setAddSerial(addSerial); sendReq.setSign(sign); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(sendReq.getEcName()); stringBuffer.append(sendReq.getApId()); stringBuffer.append(sendReq.getSecretKey()); stringBuffer.append(sendReq.getMobiles()); stringBuffer.append(sendReq.getContent()); stringBuffer.append(sendReq.getSign()); stringBuffer.append(sendReq.getAddSerial()); sendReq.setMac(Md5Util.MD5(stringBuffer.toString()).toLowerCase()); String reqText = JSON.toJSONString(sendReq); String encode = Base64.encodeBase64String(reqText.getBytes("UTF-8")); String resStr = sendPost(url, encode); SendRes sendRes = JSON.parseObject(resStr, SendRes.class); if (sendRes.isSuccess() && !"".equals(sendRes.getMsgGroup()) && "success".equals(sendRes.getRspcod())) { return 1; } else { return 0; } } // main方法测试发送短信,返回1表示成功,0表示失败 public static void main(String[] args) throws IOException { String msg = "这是发送短信的内容!"; int result = sendMsg("15182792202", msg); System.out.println("===" + result); if (result!=1) { System.out.println("生成系统日志"); }else { System.out.println("发送成功!"); } } /** * 向指定 URL 发送POST方法的请求 * * @param url 发送请求的 URL * @param param 请求参数 * @return 所代表远程资源的响应结果 */ private static String sendPost(String url, String param) { OutputStreamWriter out = null; BufferedReader in = null; String result = ""; try { URL realUrl = new URL(url); URLConnection conn = realUrl.openConnection(); conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("contentType", "utf-8"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setDoOutput(true); conn.setDoInput(true); out = new OutputStreamWriter(conn.getOutputStream()); out.write(param); out.flush(); in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += "\n" + line; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } }