java代码实现:
1、首先创建发送验证码的类
public class SendMeg {
private static final String SERVER_URL = "https://api.netease.im/sms/sendcode.action";//请求的URL
private static final String APP_KEY = "****************";//网易云分配的账号
private static final String APP_SECRET = "***********";//密码
// private static final String MOULD_ID="3057527";//模板ID
private static final String NONCE = "123456";//随机数
//验证码长度,范围4~10,默认为4
private static final String CODELEN = "6";
public static boolean sendMsg(String phone) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post = new HttpPost(SERVER_URL);
String curTime = String.valueOf((new Date().getTime() / 1000L));
String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);
//设置请求的header
post.addHeader("AppKey", APP_KEY);
post.addHeader("Nonce", NONCE);
post.addHeader("CurTime", curTime);
post.addHeader("CheckSum", checkSum);
post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
//设置请求参数
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("mobile", phone));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
//执行请求
HttpResponse response = httpclient.execute(post);
String responseEntity = EntityUtils.toString(response.getEntity(), "utf-8");
//判断是否发送成功,发送成功返回true
// String code = JSON.parseObject(responseEntity).getString("code");
if (responseEntity.equals("{"code":200}")) {
return true;
}
//System.out.println(re);
return false;
}
2、创建计算并获取checkSum的类
public class CheckSumBuilder {
//计算并获取checkSum
public static String getCheckSum(String appSecret,String nonce,String curTime){
return encode("SHA",appSecret+nonce+curTime);
}
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 sb=new StringBuilder(len*2);
for(int $i=0;$i<len;$i++){
sb.append(HEX_DIGITS[(bytes[$i]>>4)&0x0f]);
sb.append(HEX_DIGITS[bytes[$i]&0x0f]);
}
return sb.toString();
}
private static final char[] HEX_DIGITS={'0','1','2','3','4','5','6',
'7','8','9','a','b','c','d','e','f'};
}
3、创建校验工具类
public class MobileMessageCheck {
private static final String SERVER_URL = "https://api.netease.im/sms/verifycode.action";//校验验证码的请求路径URL
private static final String APP_KEY = "*****************";//网易云信分配的账号
private static final String APP_SECRET = "**************";//网易云信分配的密钥
private static final String NONCE = "123456";//随机数
public static boolean checkMsg(String phone, String sum) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post = new HttpPost(SERVER_URL);
String curTime = String.valueOf((new Date().getTime() / 1000L));
String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);
//设置请求的header
post.addHeader("AppKey", APP_KEY);
post.addHeader("Nonce", NONCE);
post.addHeader("CurTime", curTime);
post.addHeader("CheckSum", checkSum);
post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
//设置请求参数
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("mobile", phone));
nameValuePairs.add(new BasicNameValuePair("code", sum));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
//执行请求
HttpResponse response = httpclient.execute(post);
String responseEntity = EntityUtils.toString(response.getEntity(), "utf-8");
//判断是否发送成功,发送成功返回true
// String code = JSON.parseObject(responseEntity).getString("code");
if (responseEntity.equals("{"code":200}")) {
return true;
}
//System.out.println(re);
return false;
}
}