【JAVA学习】阿⾥云短信验证接⼊过程详解(附jar包下载地
址)
1,⾸先你得新建⼀个maven项⽬,在项⽬中的pom⽂件中导⼊依赖。
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.2.3</version>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
2,在导⼊时,你可能会出现导⼊失败的可能,因为阿⾥云没有把这个jar包放进maven的库中,这时候你就需要⼿动导⼊,你在
打开这个按钮
点击L开头的那个
点击添加按钮
然后就导⼊成功了
再看实现接⼝的过程
package fehead;
SimpleDateFormat;
import java.util.Date;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.del.v20170525.QuerySendDetailsRequest; import com.del.v20170525.QuerySendDetailsResponse; import com.del.v20170525.SendSmsRequest;
import com.del.v20170525.SendSmsResponse;
import ptions.ClientException;
java并发处理import ptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class SmsDemo {
//产品名称:云通信短信API产品,开发者⽆需替换
codeblocks教程视频static final String product="Dysmsapi";
//产品域名,开发者⽆需替换
static final String domain = "dysmsapi.aliyuncs";
// TODO 此处需要替换成开发者⾃⼰的AK(在阿⾥云访问控制台寻),下⾯举个例⼦ static final String accessKeyId = "LTg";
float类型长度static final String accessKeySecret = "8UvNTo5";
public static SendSmsResponse sendSms() throws ClientException{
vb简单程序源代码怎么生成//可⾃助调整超时时间
System.setProperty("sun.client.defaultConnectTimeout", "10000");
System.setProperty("sun.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不⽀持region化
IClientProfile Profile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient=new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-⽂档部分内容
SendSmsRequest request=new SendSmsRequest();
//必填:待发送⼿机号
request.setPhoneNumbers("");
/
/必填:短信签名-可在短信控制台中到举个例⼦
request.setSignName("堆栈科技");
//必填:短信模板id-可在短信控制台中到,是id不是名字,举个例⼦
request.setTemplateCode("SMS_171112301");
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为--必填,与模板相对应 //request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}");
request.setTemplateParam("{\"code\":\"666666\"}");
//选填-上⾏短信扩展码(⽆特殊需求⽤户请忽略此字段)
//request.setSmsUpExtendCode("90997");
//可选:outId为提供给业务⽅扩展字段,最终在短信回执消息中将此值带回给调⽤者
//request.setOutId("yourOutId");
//hint 此处可能会抛出异常,注意catch
SendSmsResponse AcsResponse(request);
return sendSmsResponse;
}
public static QuerySendDetailsResponse querySendDetails(String bizId) throws ServerException, ClientException{
//可⾃助调整超时时间
System.setProperty("sun.client.defaultConnectTimeout", "60000");
System.setProperty("sun.client.defaultReadTimeout", "60000");
//初始化acsClient,暂不⽀持region化
IClientProfile profile = Profile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象
QuerySendDetailsRequest request = new QuerySendDetailsRequest();
//必填-号码
request.setPhoneNumber("");
//可选-流⽔号
request.setBizId(bizId);
//必填-发送⽇期⽀持30天内记录查询,格式yyyyMMdd
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
request.setSendDate(ft.format(new Date()));
//必填-页⼤⼩
request.setPageSize(10L);
//必填-当前页码从1开始计数
request.setCurrentPage(1L);
java下载过程//hint 此处可能会抛出异常,注意catch
QuerySendDetailsResponse querySendDetailsResponse = AcsResponse(request); return querySendDetailsResponse;
}
public static void main(String[] args) throws ClientException, InterruptedException {
//发短信
SendSmsResponse response = sendSms();
Thread.sleep(3000L);
//查明细
Code() != null && Code().equals("OK")) {
System.out.println("请求成功");
QuerySendDetailsResponse querySendDetailsResponse = BizId());
System.out.println("短信明细查询接⼝返回数据----------------");
System.out.println("Code=" + Code());
System.out.println("Message=" + Message());
int i = 0;
for(QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : SmsSendDetailDTOs())
{
System.out.println("SmsSendDetailDTO["+i+"]:");
System.out.println("Content=" + Content());
System.out.println("ErrCode=" + ErrCode());
System.out.println("OutId=" + OutId());
System.out.println("PhoneNum=" + PhoneNum());
System.out.println("ReceiveDate=" + ReceiveDate());
System.out.println("SendDate=" + SendDate());
System.out.println("SendStatus=" + SendStatus());
System.out.println("Template=" + TemplateCode());
}
System.out.println("TotalCount=" + TotalCount());
System.out.println("RequestId=" + RequestId());
}
}
}
然后如果你可以在控制台上看到你发的信息,就说明你发送成功了。。。。。就是这么快,,,,你在根据具体项⽬要求更改接⼝设置就完美了。。。。。。。。。。。。。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论