騰訊雲短信接口服務

作個小程序須要發送驗證碼,短信接口是騰訊雲的。瞭解官方的sdk和demo發現對於我這種淺層次的人來講太麻煩了,而後就從網上找了一版java

開發準備

1. 申請 SDK AppID 以及 App Key:
在開始本教程以前,您須要先獲取 SDK AppID 和 App Key,如您還沒有申請,請到 短信控制檯 中添加應用。應用添加成功後您將得到 SDK AppID 以及 App Key。git

注意:
SDK AppID 是以 14xxxxx 開頭。json

2. 申請簽名:
下發短信必須攜帶簽名,您能夠在短信 控制檯 中申請短信簽名小程序

3. 申請模板:
下發短信內容必須通過審覈,您能夠在短信控制檯中申請短信模板app

完成以上三項即可開始代碼開發。dom

詳情諮詢:https://cloud.tencent.com/document/product/382/13613ui

須要的依賴this

        <dependency>
            <groupId>com.qcloud</groupId>
            <artifactId>qcloud-java-sdk</artifactId>
            <version>2.0.1</version>
        </dependency>

固然你也須要去騰訊雲註冊APPID和appkey,選擇所須要的模板,單發或者羣發,個人是單髮指定模板的。url

import com.qcloud.Utilities.Json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class TencentSmsSender {
    Random random = new Random();
    int sdkappid ;
    String appkey;
    // 請根據咱們的說明文檔適時調整 url
    final String url = "https://yun.tim.qq.com/v3/tlssmssvr/sendsms";


    public TencentSmsSender(int sdkappid, String appkey) {
        this.sdkappid = sdkappid;
        this.appkey = appkey;
    }


    //        "sign": "騰訊雲", //短信簽名,若是使用默認簽名,則能夠缺省此字段
    public String  sendMsg(String nationCode, String phoneNumber, String content) {
        long rnd = random.nextInt(999999) % (999999 - 100000 + 1) + 100000;
        String wholeUrl = String.format("%s?sdkappid=%d&random=%d", url, sdkappid, rnd);
        String pnum = null;
        try {
            URL object = new URL(wholeUrl);
            HttpURLConnection con = (HttpURLConnection) object.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestMethod("POST");
            JSONObject data = new JSONObject();
            JSONObject tel = new JSONObject();
            tel.put("nationcode", nationCode);
            String phone = phoneNumber;
            tel.put("phone", phone);
            data.put("type", "0");
            data.put("tpl_id", );//正文ID
            pnum = String.valueOf(rnd);
            List<String> places = Arrays.asList(pnum, "5");隨機生成6位數,間隔5分鐘
            data.put("params", places);

            String sig = stringMD5(appkey.concat(phone));
            data.put("sig", sig);
            data.put("tel", tel);
            //data.put("sign", "");
            data.put("extend", "");
            data.put("ext", "");

            OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "utf-8");
            wr.write(data.toString());
            wr.flush();
            // 顯示 POST 請求返回的內容
            StringBuilder sb = new StringBuilder();
            int HttpResult = con.getResponseCode();
            if (HttpResult == HttpURLConnection.HTTP_OK) {
                BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                br.close();
                System.out.println("" + sb.toString());
            } else {
                System.out.println(con.getResponseMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pnum;
    }


    private static String stringMD5(String input) throws NoSuchAlgorithmException {
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        byte[] inputByteArray = input.getBytes();
        messageDigest.update(inputByteArray);
        byte[] resultByteArray = messageDigest.digest();
        return byteArrayToHex(resultByteArray);
    }

    private static String byteArrayToHex(byte[] byteArray) {
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] resultCharArray = new char[byteArray.length * 2];
        int index = 0;
        for (byte b : byteArray) {
            resultCharArray[index++] = hexDigits[b >>> 4 & 0xf];
            resultCharArray[index++] = hexDigits[b & 0xf];
        }
        return new String(resultCharArray);
    }

    public static void main(String[] args) {


    }

}
相關文章
相關標籤/搜索