调用接口实现短信验证

匿名 (未验证) 提交于 2019-12-03 00:27:02

package com.demo;  import java.text.SimpleDateFormat; import java.util.Date;  import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest; import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile;  public class SmsUtils {      /**      * @param args      */     public static void main(String[] args) {         try {             SmsUtils smsUtils = new SmsUtils();             String telphoneString = "13168759270";             smsUtils.sendSms(telphoneString);         } catch (ClientException e) {             e.printStackTrace();         }      }          //随机验证码     private int code;     public int getCode(){         return code;     }     public void setCode(){         code = (int)(Math.random()*9999)+1000;  //每次调用生成一次四位数的随机数     }          //初始化ascClient需要的几个参数     static final String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改)     static final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改)          //替换成你的AK     static final String accessKeyId = "LTAIF7iyP8nt4UtZ";//你的accessKeyId,参考本文档步骤2     static final String accessKeySecret = "v9HX4yNJAGZoxOclDS96GzFClT926y";//你的 accessKeySecret      public SendSmsResponse sendSms(String telphone) throws ClientException{         //设置超时时间         System.setProperty("sun.net.client.defaultConnectTimeout", "10000");         System.setProperty("sun.net.client.defaultReadTimeout", "10000");         //初始化ascClient         IClientProfile profile=DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);         DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou",product, domain);         IAcsClient acsClient=new DefaultAcsClient(profile);                  //组装请求对象         SendSmsRequest request=new SendSmsRequest();         //使用post提交         request.setMethod(MethodType.POST);         //待发送的手机号         request.setPhoneNumbers(telphone);         //短信签名         request.setSignName("");         //短信模板ID         request.setTemplateCode("SMS_137620029");         //验证码         SmsUtils sms = new SmsUtils();         sms.setCode();         String codetemp = sms.getCode()+"";         System.out.println("code:        "+codetemp);         /*          * 可选:模板中的变量替换JSON串,          * 如模板内容为"亲爱的${name},您的验证码为${code}"时,          * 此处的值为{"name":"Tom","code":"1454"}          *   \  反斜杠为转义字符,使得输出双引号          */         request.setTemplateParam("{\"name\":\"Tom\", \"code\":\""+codetemp+"\"}");         //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者         //request.setOutId("1454");         SendSmsResponse response=acsClient.getAcsResponse(request);         System.out.println(response.getCode() +"========="+ response.getMessage());         if(response.getCode() != null && response.getCode().equals("OK")) {             //请求成功             System.out.println("发送成功!");         }else {             System.out.println("发送失败!");         }         return response;     } }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!