代碼:java 目的:記錄、梳理短信接入 分爲:下行短信、上行短信、狀態報告java
下行:app
//1 Http請求參數: Map<String, Object> params = new HashMap<String, Object>(); params.put("userId", smsIssuer.getAccount()); params.put("password", smsIssuer.getPwd()); params.put("pszMobis", mobileNos); if (object != null && object.containsKey("urlencode") && object.getBoolean("urlencode")) { params.put("pszMsg", URLEncoder.encode(smsInfo.getContent())); } else { params.put("pszMsg", (smsInfo.getContent())); } params.put("iMobiCount", mobiles.length); params.put("pszSubPort", "*"); //2 將以上參數根據夢網接入的接口,發起Http POST請求至夢網URL
上行: //1 參數處理:自接收到夢網上行推送以後編碼
//需對賬號密碼作校驗 String userName = request.get("userid"); String passWord = request.get("pwd"); String cmd = request.get("cmd"); String seqid = request.get("seqid"); String mos = request.get("mos"); String[] mosArray = mos.split(","); List<SmsInfo> smsInfos = new ArrayList<SmsInfo>(mosArray.length); smsMerchantReceiveSmsResponse.setSmsInfos(smsInfos); if (mosArray != null && mosArray.length > 0) { for (String moss : mosArray) { try { String[] arr = moss.split("\\|"); String moTime = arr[0];// 上行時間 String moSeq = arr[1]; // 上行流水號 String mobileNo = arr[2];// 手機號 String upNo = arr[3];// 通道號 String extNo = arr[4];// 擴展號 String codeType = arr[5];// 上行信息編碼 String content = arr[6];// 上行信息內容 if ("15".equals(codeType)) { // 15:GBK編碼 8:UNICODE編碼 content = new String(Base64.decodeBase64(content), "GBK"); } else if ("8".equals(codeType)) { content = decodeUnicode(content); } SmsInfo smsInfo = new SmsInfo(); smsInfo.setContent(content); smsInfo.setMobileNo(mobileNo); smsInfo.setReportDate(DateUtils.parseDate(moTime, "yyyy-MM-dd HH:mm:ss")); smsInfo.setExtend("mos=" + moSeq + ",upn=" + upNo + ",exn=" + extNo + ",cot=" + codeType); smsInfos.add(smsInfo); } catch (Exception e) { logger.error("", e); } finally { logger.debug("receive-sms:" + moss); } } } //生成夢網響應串 Map<String, String> params = new LinkedHashMap<String, String>(); params.put("cmd", "MO_RESP"); params.put("seqid", seqid); params.put("ret", "0"); String responseData = generateMengwReturnString(params);
狀態報告接收: //1 參數處理:自接收到夢網狀態報告推送以後url
//需對賬號密碼作校驗 String userName = request.get("userid"); String passWord = request.get("pwd"); String cmd = request.get("cmd"); String seqid = request.get("seqid"); String rpts = request.get("rpts"); String[] rptsArray = rpts.split(","); List<SmsInfo> smsInfos = new ArrayList<SmsInfo>(rptsArray.length); smsMerchantSendReportResponse.setSmsInfos(smsInfos); if (rptsArray != null && rptsArray.length > 0) { for (String moss : rptsArray) { try { String[] arr = moss.split("\\|"); String moTime = arr[0];// 狀態報告時間 String moSeq = arr[1];// 短信平臺流水號 String moSmsSeq = arr[2];// 自編流水號 String mobileNo = arr[3];// 手機號 String upNo = arr[4];// 通道號 String extNo = arr[5];// 擴展號 String state = arr[6];// 接收狀態 0:成功 2:發送失敗 String errormsg = arr[7];// 7個字節錯誤碼。DELIVRD爲成功,其餘爲失敗。 SmsInfo smsInfo = new SmsInfo(); // smsInfo.setMobileNo(mobileNo); smsInfo.setReportDate(DateUtils.parseDate(moTime, "yyyy-MM-dd HH:mm:ss")); smsInfo.setExtend("m=" + mobileNo + ",wns=" + moSmsSeq + ",upn=" + upNo + ",exn=" + extNo + ",em=" + errormsg + ",st=" + state); smsInfo.setMsgId(moSeq); smsInfo.setReportState("0".equals(state) ? errormsg : (state + "-" + errormsg)); smsInfos.add(smsInfo); } catch (Exception e) { logger.error("", e); } finally { logger.debug("report-state:" + moss); } } } //生成夢網響應串 Map<String, String> params = new LinkedHashMap<String, String>(); params.put("cmd", "RPT_RESP"); params.put("seqid", seqid); params.put("ret", "0"); String responseData = generateMengwReturnString(params);
以上公共類定義:debug
//SmsInfo 短信記錄 /* */ private long id; /* */ private long appId; /* */ private String appIp; /* */ private String appType; /* */ private String clientIp; /* */ private long issuerId; /* */ private String type; /* */ private String mobileNo; /* */ private String content; /* */ private String handleState; /* */ private String msgId; /* */ private int chargeCount; /* */ private String reportState; /* */ private Date reportDate; /* */ private int reportCheckSt; /* */ private Date create; /* */ private String extend; /* */ private boolean longMsg; /* */ private int totalCount; /* */ private int currentCount; /* */ private String remoteIp; /* */ private long actIssuerId; //SmsIssuer 短信運營商 /* */ private long id; /* */ private String name; /* */ private String account; /* */ private String pwd; /* */ private String seed; /* */ private String signature; /* */ private String smsSuffix; /* */ private int priority; /* */ private int probability; /* */ private int support; /* */ private String netType; /* */ private String limitIp; /* */ private String reportType; /* */ private String balanceUrl; /* */ private String balanceInfo; /* */ private String reportUrl; /* */ private String reportInfo; /* */ private String upUrl; /* */ private String upInfo; /* */ private String downUrl; /* */ private String downInfo; /* */ private String state; /* */ private Date create; /* */ private Date update; /* */ private String extend;