启瑞云发送短信的方法(具体描述)

偶然发现这个资源要20个积分了。。。这里直接把代码扔出来(附具体步骤)
1.在启瑞云控制台上新增api 账号
2.签名报备(新增签名)
3.签名审核成功后即可编写代码(发送方法)

下面把列出具体步骤截图:
1.在启瑞云控制台上新增api 账号
启瑞云发送短信的方法(具体描述)_第1张图片
2.签名报备(新增签名)
点击签名报备按钮,输入自己的短信模板(如图)

启瑞云发送短信的方法(具体描述)_第2张图片
等待审核审核成功后即可进行代码操作
这里使用的maven(如果不是maven ,可直接到maven仓库下载jar包),需要引入两个依赖

		
			org.apache.httpcomponents
			httpclient
			4.5.2
		
		
		
			org.apache.httpcomponents
			httpcore
			4.4.4
		

然后代码如下:

package com.ruoyi.common.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


import java.net.URLEncoder;

public class SendMessageUtils {

    //当然此配置可写在公共配置里面
    private  static  String apiKey="2250760****0";
    private  static  String apiSecret="92685c4d2cdd6*****";
    //发送的配置
    private static String url  = "http://api.qirui.com:7891/mt";

    public static String sendMessageUtil(String mobile,String msg)  {
        String respStr = null;
        try{
            String message   = msg;
            StringBuilder sb = new StringBuilder(2000);
            sb.append(url);
            sb.append("?dc=15");
            sb.append("&sm=").append(URLEncoder.encode(message, "utf8"));
            sb.append("&da=").append(mobile);
            sb.append("&un=").append(apiKey);
            sb.append("&pw=").append(apiSecret);
            sb.append("&tf=3&rd=1&rf=2");   //短信内容编码为 urlencode+utf8

            String request = sb.toString();

            CloseableHttpClient client = HttpClients.createDefault();

            HttpGet httpGet = new HttpGet(request);

            CloseableHttpResponse response = client.execute(httpGet);

            HttpEntity entity = response.getEntity();
            if(entity != null) {
                respStr = EntityUtils.toString(entity, "UTF-8");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  respStr;
    }

    public static void main(String[] args) {
       String msg=SendMessageUtils.sendMessageUtil("18**271**06","【广州平台】:您的验证码是:1102");//后面的1102 可使用方法自己生成验证码。
    }

}

欧克,记录到这了,如果帮助到您了,请点个赞再走呗!哈~

你可能感兴趣的:(java)