当前位置: 首页 > article >正文

短信宝接入发送短信实测 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);

效果示例

 


http://www.kler.cn/a/4455.html

相关文章:

  • centos安装golang
  • 【Rust练习】28.use and pub
  • STM32-keil安装时遇到的一些问题以及解决方案
  • WordPress如何配置AJAX以支持点击加载更多?
  • 【AIGC】SYNCAMMASTER:多视角多像机的视频生成
  • 【Go】Go Gin框架初识(一)
  • 【操作系统】一文带你深入浅出零拷贝技术
  • Apache POI 入门·第一话
  • Autosar诊断-简介和功能概述
  • 网络基础知识
  • 应用层 —— HTTP协议
  • Spark大数据处理讲课笔记2.3 Spark运行架构与原理
  • Android的虚拟机垃圾回收解析,简单明了看完通透
  • Jython
  • 1652_MIT 6.828 shell例程重定向的实现分析
  • mysql隐式转换 “undefined“字符串匹配到mysql int类型0值字段
  • MySQL——锁
  • K8s CrashLoopBackOff 如何排障?
  • 如何注册Shopify商店
  • 汇编语言(1)——寄存器
  • 编写python的smtplib库发送邮件代码(简洁-原创)
  • js中generator详解
  • C++之引用
  • 类ChatGPT平台推荐【国内访问ChatGPT4】
  • eNSP 本地AAA配置实验
  • <class ‘sensor_msgs.msg._Image.Image‘>原理