Java验证码工具类

package com.itheima.utils;
import com.aliyuncs.http.HttpResponse;
import org.apache.http.util.EntityUtils;

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

/**
 * 短信发送工具类
 */
public class SMSUtils {
    //发送短信
    public static void sendMessage(String mobile) {
        String host = "https://jumsendsms.market.alicloudapi.com";
        String path = "/sms/send-upgrade";
        String method = "POST";
        String appcode = "f6ec88d853ce452c9fb8262ae68feb6e";
        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>();
        querys.put("mobile", mobile);
        querys.put("templateId", "M105EABDEC");
        querys.put("value", ValidateCodeUtils.generateValidateCode(6)+"");
        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 = (HttpResponse) HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            System.out.println(EntityUtils.toString(((org.apache.http.HttpResponse) response).getEntity()));
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

 }
posted @ 2022-11-09 20:16  ajajaz  阅读(20)  评论(0编辑  收藏  举报