200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > 阿里云的短信服务acsClient+java

阿里云的短信服务acsClient+java

时间:2021-06-09 04:08:13

相关推荐

阿里云的短信服务acsClient+java

1. 开通aliyun上的短信服务,主要得到

(1)访问秘钥 accessKeyId <==>AccessKeySecret

(2)短信签名+模板code

参考官方文档

2. API测试短信发送:

参考官方文档

(1)下载sdk (原版)

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

(2) 测试

import monRequest;import monResponse;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;/*pom.xml<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.5.16</version></dependency>*/public void sendSms( ) {// 1. 初始化acsClientDefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); IAcsClient client = new DefaultAcsClient(profile);// 2. 组装请求对象/***http(s):///?Action=SendSms&PhoneNumbers=1381111*****&SignName=阿里云&TemplateCode=SMS_1530****&<公共请求参数>***/CommonRequest request = new CommonRequest();request.setSysMethod(MethodType.POST);request.setSysDomain(""); // urlrequest.setSysVersion("-05-25");request.setSysAction("SendSms");/*****/request.putQueryParameter("RegionId", "cn-hangzhou");request.putQueryParameter("PhoneNumbers", phone); // 待发送手机号request.putQueryParameter("SignName", "aliyun"); // 短信签名request.putQueryParameter("TemplateCode", "SMS_200191424"); // 短信模板code/**填充短信模板**/Map<String, String> params = new HashMap<>();params.put("station", "station");params.put("channel", "channel");params.put("alarm", "alarm");params.put("message", "message");request.putQueryParameter("TemplateParam", JSON.toJSONString(params)); // 短信模板变量对应的实际值// 3. 发送请求try {CommonResponse response = client.getCommonResponse(request); log.info(response.getData());} catch (Exception e) {log.error("短信发送失败:", e);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。