腾讯云短信发送工具类

package cn.flyinke.jtest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.HttpKit;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class QcloudSmsUtil {

    private static int sdkappid = 123;
    private static String appkey = "abc";
    private static String sign = "签名";
    private static String tplId = "123456";

    private static boolean sendMsg(String mobile, String content) {
        String url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
        Random random = new Random();
        long rnd = random.nextInt(999999) % (999999 - 100000 + 1) + 100000;
        long time = new Date().getTime()/1000;
        String wholeUrl = String.format("%s?sdkappid=%d&random=%d", url, sdkappid, rnd);
        String pnum = null;
        try {
            JSONObject data = new JSONObject();
            JSONObject tel = new JSONObject();
            tel.put("nationcode", "86");
            tel.put("mobile", mobile);

            List params = Arrays.asList(content);
            String sig = getSign(appkey,rnd,time,mobile);

            data.put("tpl_id", tplId);
            data.put("sign", sign);
            data.put("sig", sig);
            data.put("params", params);
            data.put("tel", tel);
            data.put("time",time);
            data.put("extend", "");
            data.put("ext", "");

            String response = HttpKit.post(wholeUrl,data.toJSONString());
            JSONObject rsp = JSON.parseObject(response);
            if(rsp.getInteger("result") == 0){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * sha256(appkey=5f03a35d00ee52a21327ab048186a2c4&random=7226249334&time=1457336869&mobile=13788888888)
     * = ecab4881ee80ad3d76bb1da68387428ca752eb885e52621a3129dcf4d9bc4fd4
     * @param appkey
     * @param rnd
     * @param time
     * @param mobile
     * @return
     */
    private static String getSign(String appkey,long rnd,long time,String mobile){
        return HashKit.sha256(String.format("appkey=%s&random=%d&time=%d&mobile=%s",appkey,rnd,time,mobile));
    }

    public static void main(String[] args) {
        QcloudSmsUtil.sendMsg("","1234");
    }
}

 

你可能感兴趣的:(java,腾讯云)