在做項目的時候很多時候再進行注冊的時候,我們必不可少的會用到用手機号碼進行注冊,但有些人不知道怎麼進行發送,下面我介紹一種發送驗證碼的API,這種方法僅限于個人開發項目用于練手時可以使用,如果需要用到公司項目中去的話,不建議。
在這裡我們用到了一個營運商--“秒滴”,我們通過注冊他們的賬号,可以獲得他們的接口(API)使用權,然後就可以使用短信驗證技術了,具體的JAVA實作代碼如下:
//以下三個值是通過注冊秒滴得到的個人ID值的
public static String ACCOUNT_SID = "**************";
public static String AUTH_TOKEN = "**************";
public static String REST_URL = "****************";
}
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class SMSSafetyVerificationUtil {
private static String REST_URI = "/industrySMS/sendSMS";
private static String getTimestamp(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String time = sdf.format(new Date());
return time;
}
private static String getSig(){
char[] hexValue = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
String timeStamp = getTimestamp();
try {
String all_str = SMSSafetyVerificationConfig.ACCOUNT_SID + SMSSafetyVerificationConfig.AUTH_TOKEN + timeStamp;
// String str = DigestUtils.md5Hex(all_str); //不用這種方法
MessageDigest md = MessageDigest.getInstance("md5");
byte[] bs = md.digest(all_str.getBytes());
char[] cs = new char[bs.length*2];
int k = 0;
for (int i = 0; i < bs.length; i++) {
cs[k++] = hexValue[bs[i] >>> 4 &0xf];
cs[k++] = hexValue[bs[i] & 0xf];
}
return new String(cs);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getPostContent(){
return "×tamp="+getTimestamp()+"&sig="+getSig()+"&respDataType=JSON";
}
public static String execute(String to,String num){
String body = "accountSid="+SMSSafetyVerificationConfig.ACCOUNT_SID+"&smsContent="
+"【雲購】注冊驗證碼:["+num+"],如非本人操作,請忽略此短信。"
+"&to="+to;
body += getPostContent();
try {
URL url = new URL(SMSSafetyVerificationConfig.REST_URL+REST_URI);
HttpURLConnection hConnection = (HttpURLConnection) url.openConnection();
hConnection.setRequestMethod("POST");
hConnection.setDoInput(true);
hConnection.setDoOutput(true);
hConnection.setConnectTimeout(6000);
hConnection.setReadTimeout(60000);
OutputStreamWriter out = new OutputStreamWriter(hConnection.getOutputStream(), "UTF-8");
out.write(body);
out.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(hConnection.getInputStream()));
char[] cs = new char[1024*1024];
int len = -1;
StringBuffer sb = new StringBuffer();
while((len = br.read(cs)) != -1){
sb.append(String.copyValueOf(cs, 0, len));
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getRandomNum(){
Random random = new Random();
String i = "";
while(i.length() != 6){
i = random.nextInt(1000000)+"";
}
return i;
}
}
public static void main(String[] args) {
String to = "183********"; //這是接收驗證碼的手機号碼
String num = SMSSafetyVerificationUtil.getRandomNum();
String str = SMSSafetyVerificationUtil.execute(to,num);
System.out.println(str);
}
}