阿里云实现短信验证

阿里云实现短信验证
1.进入阿里云:搜索API接口平台
搜索API接口

2.搜索:短信验证
API接口平台搜索短信验证
选中其中一个
选中一个
点击购买:
购买五次测试!

往下面继续浏览看一下所需参数
查看参数

3.由于我们是java开发所以选中java:

下面是代码
public static void main(String[] args) {
String host = “https://cdcxdxjk.market.alicloudapi.com”;
String path = “/chuangxin/dxjk”;
String method = “POST”;
String appcode = “你自己的AppCode”;
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put(“Authorization”, "APPCODE " + appcode);
Map<String, String> querys = new HashMap<String, String>();
//测试可用默认短信模板,测试模板为专用模板不可修改,如需自定义短信内容或改动任意字符,请联系旺旺或QQ726980650进行申请
querys.put(“content”, “【创信】你的验证码是:5873,3分钟内有效!”);
querys.put(“mobile”, “13568813957”);
Map<String, String> bodys = new HashMap<String, String>();

try {
 /**
 * 重要提示如下:
 * HttpUtils请从
 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
 * 下载
 *
 * 相应的依赖请参照
 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
 */
 HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
 System.out.println(response.toString());
 //获取response的body
 //System.out.println(EntityUtils.toString(response.getEntity()));
} catch (Exception e) {
 e.printStackTrace();
}

}

4.进入上述路径https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
拷贝其中代码

https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
导入maven依赖包

5.搭建测试类
import com.xr.ava.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/DuanXin")
public class DuanXinTestController {

private static final Integer yzm = getCode();

public static Integer getCode(){
    //math:数学类
    int code = (int)(Math.random()*(9999-1000+1))+1000;
    //System.out.println(code);
    return code;
}
/*public static  void main(String []args){
    DuanXinTestController dx = new DuanXinTestController();
    Integer code = dx.getCode();
    System.out.println(code);
    dx.testDuanXin("17674529163");

}*/
@RequestMapping(value = "/testDuanXin",produces = "text/String;charset=UTF-8",method = RequestMethod.POST)
@ResponseBody
public String testDuanXin(String tel){
    //定义需要返回的数据
    String str = "";
    String host = "https://cxkjsms.market.alicloudapi.com";
    String path = "/chuangxinsms/dxjk";
    String method = "POST";
    String appcode = "0efd6ebd0a964d8086aae8b626ae1f13";
    Map<String, String> headers = new HashMap<String, String>();
    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
    headers.put("Authorization", "APPCODE " + appcode);
    Map<String, String> querys = new HashMap<String, String>();
    //测试可用默认短信模板,测试模板为专用模板不可修改,如需自定义短信内容或改动任意字符,请联系旺旺或QQ726980650进行申请

如果需要改动下面模板的内容,那么联系该QQ个人测试不允许修改,否则不会收到信息
querys.put(“content”, “【创信】你的验证码是:”+yzm+",3分钟内有效!");
querys.put(“mobile”, tel);
Map<String, String> bodys = new HashMap<String, String>();

    try {
        /**
         * 重要提示如下:
         * HttpUtils请从
         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
         * 下载
         *
         * 相应的依赖请参照
         * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
         */
        HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
        System.out.println(response.toString());
        //获取response的body
        System.out.println(EntityUtils.toString(response.getEntity()));
        //EntityUtils.toString(response.getEntity());
        return "true";
    } catch (Exception e) {
        e.printStackTrace();
    }

    return "false";
}

}

6.前台请求代码
//发送验证码
$("#testdxyz").click(function () {
var tel = $("#tel").val();
alert(tel);
$.ajax({
url:"/DuanXin/testDuanXin",
data:{“tel”:tel},
type:“POST”,
success:function(data){
console.log(data);
alert(“短信发送成功!”);
}
})
})

	//判断验证码是否输入正确
	$("#yzCode").blur(function () {
	    var srYZm = $(this).val();
	    alert(srYZm);
		$.ajax({
            url:"/DuanXin/yzCode",
            data:{"srYZm":srYZm},
            type:"POST",
            success:function(data){
                console.log(data);
                if(data=="true"){
					console.log("正确");
				}else{
                    console.log("错误");
				}
            }
		})
    })

如果遇到问题请联系本人QQ:1550156671

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值