zoukankan      html  css  js  c++  java
  • java接入创蓝253短信验证码

    说明 项目是springboot框架

    1.短信配置文件 包含验证码发送路径、用户名、密码

    chuanglan.requesturl=
    
    chuanglan.account=
    
    chuanglan.pswd=
    配置文件

    具体值 查看官网 位置查看截图 红框已经标红

    2.读取配置文件类 

    3.发送数据request实体类

    public class SmsVariableRequest
    {
      private String account;
      private String password;
      private String msg;
      private String params;
      private String sendtime;
      private String report;
      private String extend;
      private String uid;
      private String msgId;
      private String failNum;
      private String successNum;
      private String phone;
    
      public SmsVariableRequest()
      {
      }
    
      public SmsVariableRequest(String account, String password, String msg, String params)
      {
        this.account = account;
        this.password = password;
        this.msg = msg;
        this.params = params;
      }
    
      public SmsVariableRequest(String account, String password, String msg, String params, String report,String phone) {
        this.account = account;
        this.password = password;
        this.msg = msg;
        this.params = params;
        this.report = report;
        this.phone = phone;
      }
    
      public String getAccount()
      {
        return this.account;
      }
      public void setAccount(String account) {
        this.account = account;
      }
      public String getPassword() {
        return this.password;
      }
      public void setPassword(String password) {
        this.password = password;
      }
      public String getMsg() {
        return this.msg;
      }
      public void setMsg(String msg) {
        this.msg = msg;
      }
    
      public String getSendtime() {
        return this.sendtime;
      }
      public void setSendtime(String sendtime) {
        this.sendtime = sendtime;
      }
      public String getReport() {
        return this.report;
      }
      public void setReport(String report) {
        this.report = report;
      }
      public String getExtend() {
        return this.extend;
      }
      public void setExtend(String extend) {
        this.extend = extend;
      }
      public String getUid() {
        return this.uid;
      }
      public void setUid(String uid) {
        this.uid = uid;
      }
      public String getParams() {
        return this.params;
      }
      public void setParams(String params) {
        this.params = params;
      }
    
    public String getMsgId() {
        return msgId;
    }
    
    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }
    
    public String getFailNum() {
        return failNum;
    }
    
    public void setFailNum(String failNum) {
        this.failNum = failNum;
    }
    
    public String getSuccessNum() {
        return successNum;
    }
    
    public void setSuccessNum(String successNum) {
        this.successNum = successNum;
    }
    
    public String getPhone() {
        return phone;
    }
    
    public void setPhone(String phone) {
        this.phone = phone;
    }
      
    }
    发送实体类

    4.接收数据response实体类

    public class SmsVariableResponse
    {
      private String time;
      private String msgId;
      private String errorMsg;
      private String failNum;
      private String successNum;
      private String code;
    
      public String getTime()
      {
        return this.time;
      }
      public void setTime(String time) {
        this.time = time;
      }
      public String getMsgId() {
        return this.msgId;
      }
      public void setMsgId(String msgId) {
        this.msgId = msgId;
      }
      public String getErrorMsg() {
        return this.errorMsg;
      }
      public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
      }
      public String getCode() {
        return this.code;
      }
      public void setCode(String code) {
        this.code = code;
      }
      public String getFailNum() {
        return this.failNum;
      }
      public void setFailNum(String failNum) {
        this.failNum = failNum;
      }
      public String getSuccessNum() {
        return this.successNum;
      }
      public void setSuccessNum(String successNum) {
        this.successNum = successNum;
      }
    
      public String toString() {
        return "SmsVarableResponse [time=" + this.time + ", msgId=" + this.msgId + ", errorMsg=" + this.errorMsg + ", failNum=" + 
          this.failNum + ", successNum=" + this.successNum + ", code=" + this.code + "]";
      }
    接收数据实体类

    5.创蓝短信发送请求工具类

    public class ChuangLanSmsUtil
    {
      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(2000);
    
          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("UTF-8"));
          os.flush();
    
          StringBuilder sb = new StringBuilder();
          int httpRspCode = httpURLConnection.getResponseCode();
          if (httpRspCode == 200)
          {
            BufferedReader br = new BufferedReader(
              new InputStreamReader(httpURLConnection.getInputStream(), "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;
      }
    }
    发送请求工具类

    6.发送短信验证码具体代码

        @Override
        public Map<String, Object> send(String content, String mobileNumber) {
                String report = "true";
                
                content="【】您的验证码是:"+content;
                SmsVariableRequest smsVariableRequest = new SmsVariableRequest(chuanglanSmsConstants.getAccount(), chuanglanSmsConstants.getPswd(),
                        content, null, report,mobileNumber);
    
                String requestJson = JSON.toJSONString(smsVariableRequest);
                String response = ChuangLanSmsUtil.sendSmsByPost(chuanglanSmsConstants.getRequesturl(), requestJson);
    
                SmsVariableResponse smsVariableResponse = (SmsVariableResponse)JSON.parseObject(response, SmsVariableResponse.class);
    
                System.out.println("response  toString is : " + smsVariableResponse);
                if(null!=smsVariableResponse&&!"0".equals(smsVariableResponse.getCode())){
                    if(log.isInfoEnabled()){
                        log.info(smsVariableResponse);
                    }
                }
                return null;
        }
    发送短信验证码具体代码

    7.注意事项

    (1)注意短信签名 【XXXX】 必须是 创蓝短信审核通过的签名   如果是测试 可以使用【253云通讯】默认的 没有问题

    (2)创蓝短信返回的错误并不是很明确 不要只注意控制台创蓝返回的错误 注意看创蓝短信在线API

    (3)注意看在线demo不建议看离线demo 会有在线demo已经更新离线demo还未更新的情况

    (4)如果需要判断验证码是否发送成功 直接"0".equals(smsVariableResponse.getCode())  “0”即发送成功

    原创不易 转载请说明出处!!!

     

     

  • 相关阅读:
    CSS3 Animation
    css形状大全
    HTML5 表单属性
    HTML5 Input 类型
    HTML 5 服务器发送事件
    jq制作博客已存在多少天
    Java网络编程与NIO详解4:浅析NIO包中的Buffer、Channel 和 Selector
    Java网络编程和NIO详解3:IO模型与Java网络编程模型
    Java网络编程与NIO详解2:JAVA NIO 一步步构建IO多路复用的请求模型
    Java网络编程和NIO详解1:JAVA 中原生的 socket 通信机制
  • 原文地址:https://www.cnblogs.com/weitaming/p/8195523.html
Copyright © 2011-2022 走看看