联通短信平台的使用

一、短信平台使用步骤:

  1. 在短信平台增加要发送的短信内容模板

网址gd.ums86.com

使用说明:就使用情况来说,只要满足模板内容,30s内可以收到短信,成功返回0。不适用模板内容,不能发送,会返回错误代码

  2.项目中使用平台接口,编写通用接口代码

public class SmsAction extends BaseAction {
    private SequenceDAO sequenceDAO;
    /**
     * 通用发送短信的接口。
     */
    public ActionForward currencySendSMS(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        String result = "";
        String number = request.getParameter("number");//手机号码
        String content = request.getParameter("content");//短信内容
        String pwd = request.getParameter("pwd");//短信内容
        Properties prop = new Properties();
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("config/sms.properties");
        try {
            prop.load(in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String pwd2 = (String) prop.get("sms.attestation.pwd");
        if(StringUtils.isBlank(pwd) || !pwd.equals(pwd2)){
            result = "{\"result\":\"6\"}";            //认证错误
            return null;
        }

     //   String content = request.getParameter("pwd");//短信内容
        if (StringUtils.isBlank(number)) {
            //发送失败 
            result = "{\"result\":\"5\"}";
            try {
                response.getWriter().write(result);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        number = number.trim();

        MobileSms mobileSms = new MobileSms();
        mobileSms.setMobile(number);
        Integer smsId = Integer.parseInt(this.sequenceDAO.getNextId("ID"));
        Sms sms = new Sms();
        sms.setId(smsId);
        sms.setAni(number);
        sms.setSmsContent(content);
        sms.setStatus(0);
       // smsManager.insert(sms); // 记录发送短信信息

        //调用短信发送接口
        SmsScheduler smsScheduler = new SmsScheduler();
        String sendByLT = smsScheduler.sendByLT(sms); //sendByLT 返回信息
        String[] sendResult = sendByLT.split("&");

        if (sendResult[0].equals("result=0")) {
            //发送成功
            request.setAttribute("smsId",smsId);
            //request.setAttribute("sms", smsRand);
            request.setAttribute("sendTime", new java.util.Date().getTime());
            result = String.format("{\"result\":\"%s\",\"smsId\":\"%s\"}", "1", smsId.toString());
        } else {
            //发送失败 
            result = "{\"result\":\"5\"}";
        }
        try {
            response.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setSequenceDAO(SequenceDAO sequenceDAO) {
        this.sequenceDAO = sequenceDAO;
    }


}








/**
     * 通过联通接口发送短信
     * @param sms
     * @return result=0 成功
     */
    public String sendByLT(Sms sms) {
        String message = "";
        try {
            com.sniper.smsSend.cn.com.flaginfo.ws.SmsStub stub = new com.sniper.smsSend.cn.com.flaginfo.ws.SmsStub(
                    smsurl);
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            // 发送接口
            com.sniper.smsSend.cn.com.flaginfo.ws.SmsStub.Sms sms0 = new com.sniper.smsSend.cn.com.flaginfo.ws.SmsStub.Sms();
            sms0.setIn0(ide);// 企业编号
            sms0.setIn1(user);// 登录名
            sms0.setIn2(pwd);// 密码
            sms0.setIn3(sms.getSmsContent());// 短信内容
            sms0.setIn4(sms.getAni());// 手机号码
            sms0.setIn5(format.format(new Date()).toString());
            sms0.setIn6("");
            sms0.setIn7("1");
            sms0.setIn8("");
            com.sniper.smsSend.cn.com.flaginfo.ws.SmsStub.SmsResponse resp = stub.Sms(sms0);
            message = resp.getOut();
        
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message;
    }

二、调用项目通用接口步骤

因为短信平台需要IP白名单,所以不是什么IP都可以直接调用的。我们通过正式站来调用接口,这样就省去了IP备案的过程。

1. HttpClient调用
public static void main(String[] args) {
        String str = "";
        String tel = "177****0143";
        String method = "currencySendSMS";
        String content="您好!本次手机操作验证码为000000,请在30分钟内完成验证,验证码涉及到您的账户安全,请勿告知他人。如非本人操作,请忽略本短信。";
        str = HttpClient.doPost("https://www.xxxx.com/sms.do?tstmp="+Math.random(), 
                "method="+method+"&number="+tel+"&content="+content+"&pwd=samgou201904021156");
        System.out.println(str);
    }

不出意外,177****0143 就能收到短信

猜你喜欢

转载自www.cnblogs.com/jkwll/p/10789185.html