阿里云短信服务使用代码

阿里云短信服务使用代码

Scroll Down

Java代码

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class SendMsg {
    public JSONObject LoginSendMsg(String PhoneNumbers, String TemplateParam) {
        DefaultProfile profile = DefaultProfile.getProfile(“cn-hangzhou”, “<accessKeyId>”, “<accessSecret>”);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain(“dysmsapi.aliyuncs.com”);
        request.setVersion(“2017-05-25”);
        request.setAction(“SendSms”);
        request.putQueryParameter(“RegionId”, “cn-hangzhou”);
        request.putQueryParameter(“PhoneNumbers”, PhoneNumbers);//目标手机号
        request.putQueryParameter(“SignName”, “签名名称”);
        request.putQueryParameter(“TemplateCode”, “模版CODE”);
        request.putQueryParameter(“TemplateParam”, “{\”code\”:\””+TemplateParam+”\”}”);//验证码
        CommonResponse response=null;
        try {
            response = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        JSONObject result = new JSONObject(response.getData());
        System.out.println(result.getString(“Code”));
        return result;
    }

    private static Map map = new HashMap();
    public void testTime(String phone, String code) {
        JSONObject result=new SendMsg().LoginSendMsg(phone,code);//发送验证码
        if(result.getString(“Code”).equals(“OK”)){
            System.out.println(“发送成功”);
        }
        map.put(phone,code);//放入map集合进行对比
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                map.remove(phone);
                System.out.println(phone+”的时间到了删除成功”);
                timer.cancel();
            }
        }, 1  10  1000);//计时一分钟
    }

    public static void print() {
        System.out.println(map);
    }

    public static void main(String[] args) {
        Thread t1 = new Thread() {
            public void run() {
                int seconds = 0;
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    SendMsg.print();
                }
            }
        };
        t1.start();
        Thread t2 = new Thread() {
            public void run() {
                new SendMsg().testTime(“xxxxxxxxxx”,”6666”);
            }
        };
        t2.start();
    }
}

上面的代码是测试的,主函数里面有两个线程,一个给调用函数给一个手机号发送验证码,一个是死循环一直输出map集合中的键值对,然后发送函数里面有一个计时器,计时一分钟,一分钟后将从map集合中删除该验证码,即验证码失效。发送验证码的返回值有很多种这里之判断了一种就是发送成功。

maven中添加jar包

<dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-core</artifactId>
   <version>4.0.3</version>
</dependency>