Explain the project is a springboot framework
1. The SMS configuration file contains the verification code sending path, user name, and password
changlan.requesturl=changlan.account=changlan.pswd=
Configuration File
Check the specific value for the official website to view the screenshot. The red box has been marked with red
2. Read the configuration file class
3. Send data request entity class
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. Receive data response entity class
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.Chuanglan SMS Send Request Tool
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. Send SMS verification code specific code
@Override public Map<String, Object> send(String content, String mobileNumber) { String report = "true"; content="【】Your verification code is: "+content; SmsVariableRequest smsVariableRequest = new SmsVariableRequest(chuanglanSmsConstants.getAccount(), changlanSmsConstants.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. Things to note
(1) Pay attention to the SMS signature [XXXX] must be a signature approved by Chuanglan SMS. If it is a test, you can use [253 Cloud Communication] default without any problem.
(2) The error returned by Chuanglan SMS is not very clear. Don’t just pay attention to the error returned by Chuanglan on the console. Pay attention to the online API of Chuanglan SMS.
(3) Note that watching online demos is not recommended to watch offline demos. There will be cases where online demos have been updated and offline demos have not been updated yet.
(4) If you need to determine whether the verification code is successfully sent, it is "0".equals(smsVariableResponse.getCode()) "0" means that the sending is successful
The above example explanation of Java access to Chuanglan 253 SMS verification code is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.