說明 項目是springboot框架 1.簡訊配置文件 包含驗證碼發送路徑、用戶名、密碼 chuanglan.requesturl= chuanglan.account= chuanglan.pswd= 具體值 查看官網 位置查看截圖 紅框已經標紅 2.讀取配置文件類 3.發送數據request實體 ...
說明 項目是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”即發送成功
原創不易 轉載請說明出處!!!