容联云(第三方短信发送)

首先登陆官网,注册
下载它官网给的Demo,在Demo里面有一个jar包
这是官网提供的开发文档
jar包导入到项目内
我用的是私服导包的,普通导入jar包的只能手动咯
这是私服导jar包的配置教程
按照上面的步骤就能连接到私服了,localhost更换成对应的IP
这是随机生成码的代码

String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);

我用redis把验证码存进去,设置存在时间2分钟,再判断验证码是否过期

package com.bdqn.text;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Random;
import java.util.Set;

public class text {
    public static void main(String[] args) {
        HashMap result = null;
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
        restAPI.init("app.cloopen.com", "8883");
        // 初始化服务器地址和端口,生产环境配置成app.cloopen.com,端口是8883.
        restAPI.setAccount("8a216da866f71d040167017d4bad034a","d18be9243dab4571ab0e43c40b08d72b");
        restAPI.setAppId("8a216da866f71d040167017d4c000351");
        //六位随机数
        String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
        //连接redis
        Jedis jedis=new Jedis("134.175.81.174",6379);
        jedis.auth("123456");
        //2分钟销毁
        jedis.setex(verifyCode,120,verifyCode);

        result = restAPI.sendTemplateSMS("18520189286","1" ,new String[]{verifyCode,"2"});
        System.out.println("SDKTestGetSubAccounts result=" + result);
        if("000000".equals(result.get("statusCode"))){
            //正常返回输出data包体信息(map)
            HashMap data = (HashMap) result.get("data");
            Set keySet = data.keySet();
            for(String key:keySet){
                Object object = data.get(key);
                System.out.println(key +" = "+object);
            }
        }else{
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
        }

    }
}

验证是否在两分钟值内,若在redis内存在就证明通过

package com.bdqn.text;

import redis.clients.jedis.Jedis;

public class Text2 {
    public static void main(String[] args) {
        Jedis jedis=new Jedis("134.175.81.174",6379);
        jedis.auth("123456");
        String str=jedis.get("973571");
        boolean bol1=jedis.exists("973571");
        System.out.println(bol1);
    }
}

 

你可能感兴趣的:(学习)