最近在开发新的项目,因为需要使用验证码验证这个功能。我选择的是 网易云 的验证码接口。免费使用20条。并且在正式使用的过程中,对比了很多家以后,感觉还是他家的便宜。因为随着你购买的短信包数量的增加,价钱会相应的降低,下发的效率挺高的。
官方给出的demo已经是很完美了,但是我总有中使用最新插件的强迫症,随着插件的更新,可能有些函数已经被废弃或者将要废弃。给出我写的一个demo。
SendCode.java (调用成功以后,返回的json串中,obj字段对应的值即下发的内容)
public class SendCode {
public static String send(String mobile) throws Exception {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(NeteaseConsts.SERVER_URL);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
String nonce = CreateUUID.getUUID();
String checkSum = CheckSumBuilder.getCheckSum(NeteaseConsts.APP_SECRET, nonce, curTime);
// 设置请求的header
httpPost.addHeader("AppKey", NeteaseConsts.APP_KEY);
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
// 设置请求的的参数,requestBody参数
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("mobile", mobile));
nvps.add(new BasicNameValuePair("codeLen", NeteaseConsts.CODELEN));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
// 执行请求
HttpResponse response = httpClient.execute(httpPost);
String resultStr = EntityUtils.toString(response.getEntity(), "utf-8");
if (!StringUtils.isEmpty(resultStr)) {
JsonObject resultData = new JsonParser().parse(resultStr).getAsJsonObject();
String code = resultData.get("code").getAsString();
if ("200".equals(code)) {
String obj = resultData.get("obj").getAsString();
return obj;
}
}
return "";
}
}
CheckSumBuilder.java(官方给出的sha1加密方案,已经是写好的)
public class CheckSumBuilder {
public static String getCheckSum(String appSecret, String nonce, String curTime) {
return encode("sha1", 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' };
}
NeteaseConsts.java (自己写的静态类,专门放置一些固定参数)
public class NeteaseConsts {
public static final String SERVER_URL = "ease.im/sms/sendcode.action";
public static final String APP_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String APP_SECRET = "XXXXXXXXXXXXXXX";
public static final String CODELEN = "6";
}
pom.xml(搭配的gson版本,以及httpcore、httpclient版本信息)
com.google.code.gson
gson
2.8.1
org.apache.httpcomponents
httpcore
4.4.6
org.apache.httpcomponents
httpclient
4.5.3
0
相关