java 阿里云短信发送 (2)

 

java 阿里云短信发送

 

 接下来我们进行测试一下:

package com.sms; import com.alibaba.fastjson.JSONObject; 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.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.util.HashMap; import java.util.Map; @SpringBootTest class SmsApplicationTests { @Test void contextLoads() { //链接阿里云 DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "你的AccessKeyID", "你的AccessKeySecret"); IAcsClient client = new DefaultAcsClient(profile); //构建请求 CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); //不用动 request.setVersion("2017-05-25"); //不用动 request.setAction("SendSms"); // //自定义的参数(手机号,验证码,签名,模板) request.putQueryParameter("PhoneNumbers", "手机号码"); request.putQueryParameter("SignName", "你的签名名称"); request.putQueryParameter("TemplateCode", "对应的模版CODE"); //构建一个短信的验证 Map<String , Object> map = new HashMap<>(); map.put("code",1124); //这里因为是测试 所以写死了 System.out.println(JSONObject.toJSONString(map)); request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map)); //这里需要转成字符串形式 try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpysfg.html