java手机发送验证码
1.导第三方jar包。
2.编写生成验证码的类。
import java.security.MessageDigest;
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.编写测试类。
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
public class Test2018 {
//发送验证码的请求路径URL
private static final String
SERVER_URL="https://api.netease.im/sms/sendcode.action";
//网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
private static final String
APP_KEY="";//填写自己的APP_KEY
//网易云信分配的**,请替换你在管理后台应用下申请的appSecret
private static final String APP_SECRET="";//填写自己的appSecret
//随机数
private static final String NONCE="123456";
public static void main(String[] args) {
String s="";//填写要发送的手机号码
try {
String str = new Test2018().sendMsg(s);
if("success".equals(str)){
System.out.println("发送成功!");
}else{
System.out.println("发送失败!");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static String 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<>();
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");//判断是否发送成功
String obj= JSON.parseObject(responseEntity).getString("obj");//返回验证码
System.out.println(code);
if (code.equals("200")){
return "success";
}
return "error";
}
}
详细了解需参考发送验证码的开发者手册。