码迷,mamicode.com
首页 > 编程语言 > 详细

java接入创蓝253短信验证码

时间:2018-01-04 22:59:46      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:文件   uid   int   post   stack   append   override   审核   path   

说明 项目是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”即发送成功

 

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

 

 

 

java接入创蓝253短信验证码

标签:文件   uid   int   post   stack   append   override   审核   path   

原文地址:https://www.cnblogs.com/weitaming/p/8195523.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!