java接入創藍253短信驗證碼

說明 項目是springboot框架spring

1.短信配置文件 包含驗證碼發送路徑、用戶名、密碼json

chuanglan.requesturl=

chuanglan.account=

chuanglan.pswd=
配置文件

具體值 查看官網 位置查看截圖 紅框已經標紅springboot

2.讀取配置文件類 app

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實體類ide

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.發送短信驗證碼具體代碼post

 @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雲通信】默認的 沒有問題ui

(2)創藍短信返回的錯誤並非很明確 不要只注意控制檯創藍返回的錯誤 注意看創藍短信在線API

(3)注意看在線demo不建議看離線demo 會有在線demo已經更新離線demo還未更新的狀況

(4)若是須要判斷驗證碼是否發送成功 直接"0".equals(smsVariableResponse.getCode())  「0」即發送成功

 

原創不易 轉載請說明出處!!!

相關文章
相關標籤/搜索