做个小程序需要发送验证码,短信接口是腾讯云的。了解官方的sdk和demo发现对于我这种浅层次的人来说太麻烦了,然后就从网上找了一版.
国内短信新购三重礼
直达地址:http://cloud.tencent.com/act/pro/voucherslist
秒级触达,99%到达率,首次购买短信套餐包限时尊享新人大礼
开发准备
1. 申请 SDK AppID 以及 App Key: 在开始本教程之前,您需要先获取腾讯云 SDK AppID 和 App Key,如您尚未申请,请到 腾讯云短信控制台 中添加应用。应用添加成功后您将获得 SDK AppID 以及 App Key。
注意: SDK AppID 是以 14xxxxx 开头。
2. 申请签名: 腾讯云下发短信必须携带签名,您可以在短信 控制台 中申请短信签名
3. 申请模板: 腾讯云下发短信内容必须经过审核,您可以在短信控制台中申请短信模板
完成以上三项便可开始代码开发。
详情咨询:https://cloud.tencent.com/document/product/382/13613
需要的依赖
代码语言:javascript复制 <dependency>
<groupId>com.qcloud</groupId>
<artifactId>qcloud-java-sdk</artifactId>
<version>2.0.1</version>
</dependency>
当然你也需要去腾讯云注册APPID和appkey,选择所需要的模板,单发或者群发,我的是单发指定模板的。
代码语言:javascript复制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) {
}
}