秒滴科技-短信验证码接口

http://www.miaodiyun.com/

自行登录秒滴科技科技进行注册,这里就不演示了,注册后送10元短信费
ACCOUNT_SID 、 AUTH_TOKEN 这两个参数在你的账号信息里面有
SMS_CONTENT 模板内容必须保持跟注册账号的里面申请的模板保持一致,不可随便乱写

import org.apache.commons.codec.digest.DigestUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 * 发送短信验证码工具类
 * http://www.miaodiyun.com/
 */
public class SendMessageUtils {

    /**url前半部分*/
    public static final String BASE_URL = "https://api.miaodiyun.com/20150822/industrySMS/sendSMS";

    /**开发者注册后系统自动生成的账号,可在官网登录后查看*/
    public static final String ACCOUNT_SID = "******************";

    /**开发者注册后系统自动生成的TOKEN,可在官网登录后查看*/
    public static final String AUTH_TOKEN = "******************";

    /**响应数据类型, JSON或XML*/
    public static final String RESP_DATA_TYPE = "JSON";

    /**随机数*/
    public static final String RANDOM_NUM = (int)((Math.random()*9+1)*100000) + "";

    /**短信模板*/
    public static final String SMS_CONTENT = "【盛宏科技】注册验证码:"+ RANDOM_NUM +",如非本人操作,请忽略此短信。";

    /**
     * 构造通用参数timestamp、sig和respDataType
     *
     * @return
     */
    public static String createCommonParam(String sid,String token) {
        // 时间戳
        long timestamp = System.currentTimeMillis();
        // 签名
        String sig = DigestUtils.md5Hex(sid + token + timestamp);
        StringBuilder commonParam = new StringBuilder();
        commonParam.append("&timestamp=").append(timestamp);
        commonParam.append("&sig=").append(sig);
        commonParam.append("&respDataType=").append(RESP_DATA_TYPE);
        return commonParam.toString();
    }

    /**
     * post请求
     *
     * @param url
     *            功能和操作
     * @param body
     *            要post的数据
     * @return
     * @throws Exception
     */
    public static String post(String url, String body) {
        System.out.println("body:" + System.lineSeparator() + body);

        String result = "";
        try {
            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();

            // 设置连接参数
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(20000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 提交数据
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(body);
            out.flush();

            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null) {
                if (firstLine) {
                    firstLine = false;
                } else {
                    result += System.lineSeparator();
                }
                result += line;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 回调测试工具方法
     *
     * @param url
     * @return
     */
    public static String postCallBack(String url, String body) {
        String result = "";
        try {
            OutputStreamWriter out = null;
            BufferedReader in = null;
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();

            // 设置连接参数
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(20000);

            // 提交数据
            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(body);
            out.flush();

            // 读取返回数据
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line = "";
            boolean firstLine = true; // 读第一行不加换行符
            while ((line = in.readLine()) != null) {
                if (firstLine) {
                    firstLine = false;
                } else {
                    result += System.lineSeparator();
                }
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 短信发送(验证码通知,会员营销)
     * 接口文档地址:http://www.miaodiyun.com/doc/https_sms.html
     */
    public static void execute(String phone){
        try {
            StringBuilder sb = new StringBuilder();
            sb.append("accountSid").append("=").append(ACCOUNT_SID);
            sb.append("&to").append("=").append(phone);
            sb.append("&param").append("=").append(URLEncoder.encode("","UTF-8"));
            //sb.append("&templateid").append("=").append("1521");
            sb.append("&smsContent").append("=").append(URLEncoder.encode(SMS_CONTENT,"UTF-8"));
            //URLEncoder.encode("【秒嘀科技】您的验证码为123456,该验证码5分钟内有效。请勿泄漏于他人。","UTF-8")
            String body = sb.toString() + createCommonParam(ACCOUNT_SID, AUTH_TOKEN);
            String result = post(BASE_URL, body);
            System.out.println(result);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        execute("手机号码");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值