短信宝接入发送短信实测 Java
1丶注册短信宝
2 代码分为授权跟参数实体
授权实体
@Data public class SmsBaoAuthorization { //http://api.smsbao.com/sms private String url; //在短信宝注册的用户名 private String username; //在短信宝注册的密码 private String password; }
参数实体
@Data public class SmsBaoMsgData { private String phone; //签名放入内容中 也可单独作为参数拼接 例如:【万千购】您的验证码是1234 private String content; }
调用接口
@Slf4j @Component public class SmsBaoSendMsgHandle { public String sendMsgOne(SmsBaoAuthorization smsBaoAuthorization, SmsBaoMsgData smsBaoMsgData) { String state = SMSBaoUtil.send(smsBaoAuthorization, smsBaoMsgData); return state; } public void sendMsgBatch(SmsBaoAuthorization smsBaoAuthorization, List<SmsBaoMsgData> smsBaoMsgDatas) { log.info("发短信"); for (SmsBaoMsgData smsBaoMsgData : smsBaoMsgDatas) { String state = SMSBaoUtil.send(smsBaoAuthorization, smsBaoMsgData); } } }
短信宝发送工具类
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SMSBaoUtil { public static String url; public static String username; public static String password; public static void setUrl(String url) { SMSBaoUtil.url = url; } public static void setUsername(String username) { SMSBaoUtil.username = username; } public static void setPassword(String password) { SMSBaoUtil.password = password; } public static String send(SmsBaoAuthorization smsBaoAuthorization, SmsBaoMsgData smsBaoMsgData) { StringBuffer httpArg = new StringBuffer(); httpArg.append("u=").append(smsBaoAuthorization.getUsername()).append("&"); httpArg.append("p=").append(md5(smsBaoAuthorization.getPassword())).append("&"); httpArg.append("m=").append(smsBaoMsgData.getPhone()).append("&"); httpArg.append("c=").append(encodeUrlString(smsBaoMsgData.getContent(), "UTF-8")); return request(smsBaoAuthorization.getUrl(), httpArg.toString()); } public static String send(String phone, String content) { StringBuffer httpArg = new StringBuffer(); httpArg.append("u=").append(username).append("&"); httpArg.append("p=").append(md5(password)).append("&"); httpArg.append("m=").append(phone).append("&"); httpArg.append("c=").append(encodeUrlString(content, "UTF-8")); String result = request(url, httpArg.toString()); return result; } public static String send(String url, String username, String password, String phone, String content) { StringBuffer httpArg = new StringBuffer(); httpArg.append("u=").append(username).append("&"); httpArg.append("p=").append(md5(password)).append("&"); httpArg.append("m=").append(phone).append("&"); httpArg.append("c=").append(encodeUrlString(content, "UTF-8")); String result = request(url, httpArg.toString()); return result; } public static String md5(String plainText) { StringBuffer buf = null; try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(plainText.getBytes()); byte b[] = md.digest(); int i; buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return buf.toString(); } public static String encodeUrlString(String str, String charset) { String strret = null; if (str == null) return str; try { strret = java.net.URLEncoder.encode(str, charset); } catch (Exception e) { e.printStackTrace(); return null; } return strret; } public static String request(String httpUrl, String httpArg) { BufferedReader reader = null; String result = null; StringBuffer sbf = new StringBuffer(); httpUrl = httpUrl + "?" + httpArg; try { URL url = new URL(httpUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); InputStream is = connection.getInputStream(); reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); String strRead = reader.readLine(); if (strRead != null) { sbf.append(strRead); while ((strRead = reader.readLine()) != null) { sbf.append("\n"); sbf.append(strRead); } } reader.close(); result = sbf.toString(); } catch (Exception e) { e.printStackTrace(); } return result; } }
在使用类里面调用
1注入调用类
@Autowired private SmsBaoSendMsgHandle smsBaoSendMsgHandle;
2装配授权参数
SmsBaoAuthorization smsBaoAuthorization = new SmsBaoAuthorization();
3装配消息参数
SmsBaoMsgData smsBaoMsgData = new SmsBaoMsgData();
4 发送
sendResult=smsBaoSendMsgHandle.sendMsgOne(smsBaoAuthorization,smsBaoMsgData);
效果示例