开发流程:
- 生成效验
- 发送验证码
- 短信验证
一、生成CheckSum校验
import java.security.MessageDigest;
/**
* 网易云短信工具类
* @author dayun.wang
*
*/
public class CheckSumBuilder {
// 计算并获取CheckSum
public static String getCheckSum(String appSecret, String nonce, String curTime) {
return encode("lol", appSecret + nonce + curTime);
}
// 计算并获取md5值
public static String getMD5(String requestBody) {
return encode(“md5”, requestBody);
}
private static String encode(String algorithm, String value) {
if (value == null) {
return null;
}
try {
MessageDigest messageDigest
= MessageDigest.getInstance(algorithm);
messageDigest.update(value.getBytes());
return getFormattedText(messageDigest.digest());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static String getFormattedText(byte[] bytes) {
int len = bytes.length;
StringBuilder buf = new StringBuilder(len * 2);
for (int j = 0; j < len; j++) {
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
}
return buf.toString();
}
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}
二、发送验证码
1、 参数说明
2、发送验证码
APPSECRET:密钥
NONCE:随机数(自己定义)
WYYURlSEND:请求action(https://api.netease.im/sms/sendcode.action)
/**
* 发送验证码模板
*
* @author dayun.wang
* @version 1.0.0
* @param str
* 发送验证码
* @return true or false
* @throws IOException
* */
public static Integer getSendcode(String mobile) throws IOException{
String curTime = String.valueOf((new Date()).getTime() / 1000L); // 当前UTC时间戳
String checkSum = CheckSumBuilder.getCheckSum(Constant.APPSECRET, Constant.NONCE, curTime);
String params = "templateid="+Constant.WYYTEMPLATID+"&mobile="+mobile;
URL realUrl;
URLConnection conn;
PrintWriter out = null;
BufferedReader in = null;
String result = "";
// 返回码 200、301、315、403、404、413、414、500
String code="500";
try {
realUrl = new URL(Constant.WYYURlSEND);//https://api.netease.im/sms/sendcode.action
conn = realUrl.openConnection();
// 请求头文件
conn.setRequestProperty("AppKey", Constant.APPKEY);
conn.setRequestProperty("CurTime", curTime);
conn.setRequestProperty("CheckSum", checkSum);
conn.setRequestProperty("Nonce", Constant.NONCE);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
// 设置post请求
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取对象的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送参数
out.print(params);
out.flush();
// 读取响应信息
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
result += line;
}
// 获取发送状态
code = JSON.parseObject(result).getString("code").toString();
} catch (MalformedURLException e) {
e.printStackTrace();
}finally{
out.close();
in.close();
}
return Integer.valueOf(code);
}
三、短信验证
1、参数说明:
WYYURlVERIFY:请求action(https://api.netease.im/sms/verifycode.action)
/**
* 校验验证码模板
*
* @author dayun.wang
* @version 1.0.0
* @param str
* 校验验证码
* @return true or false
* @throws IOException
* */
public static Integer getVerifyCode(String mobile,String value) throws IOException{
String curTime = String.valueOf((new Date()).getTime() / 1000L); // 当前UTC时间戳
String checkSum = CheckSumBuilder.getCheckSum(Constant.APPSECRET, Constant.NONCE, curTime);
String params = "mobile="+mobile+"&code="+value;
URL realUrl;
URLConnection conn;
PrintWriter out = null;
BufferedReader in = null;
String result = "";
String code="500";
try {
realUrl = new URL(Constant.WYYURlVERIFY);
conn = realUrl.openConnection();
conn.setRequestProperty("AppKey", Constant.APPKEY);
conn.setRequestProperty("CurTime", curTime);
conn.setRequestProperty("CheckSum", checkSum);
conn.setRequestProperty("Nonce", Constant.NONCE);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(params);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
result += line;
}
code = JSON.parseObject(result).getString("code").toString();
} catch (MalformedURLException e) {
e.printStackTrace();
}finally{
out.close();
in.close();
}
return Integer.valueOf(code);
}
四、自定义短信模板
1、参数说明
WYYSENDTEMPLATE:请求action(https://api.netease.im/sms/sendtemplate.action)
/**
* 自定义模板
*
* @author dayun.wang
* @version 1.0.0
* @param str
* 重置密码
* @return true or false
* @throws IOException
* */
public static Integer getResetPassword(String mobile,String password) throws IOException{
String curTime = String.valueOf((new Date()).getTime() / 1000L); // 当前UTC时间戳
String checkSum = CheckSumBuilder.getCheckSum(Constant.APPSECRET, Constant.NONCE, curTime);
String params = "templateid="+Constant.WYYSENDTEMPLATECODE+"&mobiles=[\""+mobile+"\"]¶ms=[\""+password+"\"]";
URL realUrl;
URLConnection conn;
PrintWriter out = null;
BufferedReader in = null;
String result = "";
// 返回码 200、301、315、403、404、413、414、500
String code="500";
try {
realUrl = new URL(Constant.WYYSENDTEMPLATE);
conn = realUrl.openConnection();
conn.setRequestProperty("AppKey", Constant.APPKEY);
conn.setRequestProperty("CurTime", curTime);
conn.setRequestProperty("CheckSum", checkSum);
conn.setRequestProperty("Nonce", Constant.NONCE);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.print(params);
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
result += line;
}
code = JSON.parseObject(result).getString("code").toString();
} catch (MalformedURLException e) {
e.printStackTrace();
}finally{
out.close();
in.close();
}
return Integer.valueOf(code);
}
五、最后
20条试用短信省着点用,20条试用短信省着点用,20条试用短信省着点用,重要的事说三遍!要买得40000条开始,madan。。。