第五节 使用hutool工具发送手机短信

亮子 2023-10-15 00:55:19 13811 0 0 0

1、登录阿里云并申请功能

发送短信文档
https://market.aliyun.com/products/57126001/cmapi00037415.html?spm=5176.730005.result.8.753235249VqYNg&innerSource=search_%E7%9F%AD%E4%BF%A1#sku=yuncode31415000020

图片alt

2、短信单条发送

  • 调用地址:
    http(s)://gyytz.market.alicloudapi.com/sms/smsSend

  • 请求方式:
    POST

  • 返回类型:
    JSON

  • 请求参数(Query)

图片alt

3、官方代码示例

  • java代码
	public static void main(String[] args) {
	    String host = "https://gyytz.market.alicloudapi.com";
	    String path = "/sms/smsSend";
	    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>();
	    querys.put("mobile", "mobile");
	    querys.put("param", "**code**:12345,**minute**:5");

//smsSignId(短信前缀)和templateId(短信模板),可登录国阳云控制台自助申请。参考文档:http://help.guoyangyun.com/Problem/Qm.html

	    querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
	    querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");
	    Map<String, String> bodys = new HashMap<String, String>();


	    try {
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从\r\n\t    \t* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java\r\n\t    \t* 下载
	    	*
	    	* 相应的依赖请参照
	    	* 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();
	    }
	}
  • 正常返回示例
{
    "msg": "成功", 
    "smsid": "16565614329364584123421",  //批次号。可通过该ID查询发送状态或者回复短信。API接口可联系客服获取。
    "code": "0",
    "balance": "1234"  //账户剩余次数
}
  • 失败返回示例
{
    "code":"XXXX",
    "msg":"错误提示内容",
    "ILLEGAL_WORDS":["XX","XX"]    // 如有则显示
     // 1、http响应状态码对照表请参考:https://help.aliyun.com/document_detail/43906.html;
     // 2、如果次数用完会返回 403,Quota Exhausted,此时继续购买就可以;
     // 3、如果appCode输入不正确会返回 403,Unauthorized;
}

4、错误码定义

图片alt

5、如何查看自己的appcode

  • 点击下图按钮

图片alt

  • 复制appcode

图片alt

6、发送短信代码

  • 发送短信代码
package com.shenmazong.zg2.controller;

import cn.hutool.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

/**
 * @author 军哥
 * @version 1.0
 * @description: 发送短信Controller
 * @date 2023/10/15 8:35
 */

@Controller
@RequestMapping(value = "/sms")
public class SmsController {

    @PostMapping(value = "/send")
    @ResponseBody
    public String sendSms(String mobile, String code) {

        // 定义发送短信网关地址以及授权编码
        String url = "https://gyytz.market.alicloudapi.com/sms/smsSend";
        String appcode = "bd7b0a53b66a43c5bb2ffecd39cb9619";
        String smsSignId = "2e65b1bb3d054466b82f0c9d125465e2";
        String templateId = "908e94ccf08b4476ba6c876d13f084ad";

        // 短信发送参数
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("mobile", mobile);
        hashMap.put("param", "**code**:" + code + ",**minute**:5");
        hashMap.put("smsSignId", smsSignId);
        hashMap.put("templateId", templateId);

        // 发送短信
        String body = HttpRequest.post(url)
                .header("Authorization", "APPCODE " + appcode)
                .form(hashMap)
                .execute()
                .body();
        if (!body.contains("成功")){
            return "ERROR";
        }
        else {
            System.out.println(body);
        }
        return "OK";
    }

}
  • postman测试

图片alt