JAVA實現手機簡訊驗證碼
阿新 • • 發佈:2018-12-08
手機簡訊驗證碼介面來自於網易雲手機簡訊介面
package com.netease.code; 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.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.netease.checksum.CheckSumBuilder; /** * 傳送手機簡訊驗證碼 * @author liuxuanlin * */ public class SendCode { //傳送驗證碼的請求路徑URL private static final String SERVER_URL="https://api.netease.im/sms/sendcode.action"; //網易雲信分配的賬號,請替換你在管理後臺應用下申請的Appkey private static final String APP_KEY="c6c0e0d8f76303acc8920865f5e6d42d"; //網易雲信分配的金鑰,請替換你在管理後臺應用下申請的appSecret private static final String APP_SECRET="95df0955413b"; //隨機數 private static final String NONCE="123456"; //簡訊模板ID private static final String TEMPLATEID="9504256"; //手機號 private static final String MOBILE="18902865427"; //驗證碼長度,範圍4~10,預設為4 private static final String CODELEN="6"; public static void main(String[] args) throws Exception { HttpClient httpClient = HttpClientBuilder.create().build(); System.out.println("1111:"+httpClient); HttpPost httpPost = new HttpPost(SERVER_URL); System.out.println("2222:"+httpPost); String curTime = String.valueOf((new Date()).getTime() / 1000L); System.out.println("3333:"+curTime); /* * 參考計算CheckSum的java程式碼,在上述文件的引數列表中,有CheckSum的計算文件示例 */ String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime); System.out.println("4444:"+checkSum); // 設定請求的header httpPost.addHeader("AppKey", 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<NameValuePair> nvps = new ArrayList<NameValuePair>(); /* * 1.如果是模板簡訊,請注意引數mobile是有s的,詳細引數配置請參考“傳送模板簡訊文件” * 2.引數格式是jsonArray的格式,例如 "['13888888888','13666666666']" * 3.params是根據你模板裡面有幾個引數,那裡面的引數也是jsonArray格式 */ nvps.add(new BasicNameValuePair("templateid", TEMPLATEID)); nvps.add(new BasicNameValuePair("mobile", MOBILE)); nvps.add(new BasicNameValuePair("codeLen", CODELEN)); System.out.println("5555:"+nvps); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); // 執行請求 HttpResponse response = httpClient.execute(httpPost); /* * 1.列印執行結果,列印結果一般會200、315、403、404、413、414、500 * 2.具體的code有問題的可以參考官網的Code狀態表 */ System.out.println(EntityUtils.toString(response.getEntity(), "utf-8")); } }
package com.netease.checksum; import java.security.MessageDigest; public class CheckSumBuilder { // 計算並獲取CheckSum 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]); } System.out.println("77:"+buf.toString()); 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' }; }
以下程式碼為擷取main方法中輸入的字串(只獲取到有效的簡訊驗證碼)
String line=EntityUtils.toString(response.getEntity(), "utf-8");
Integer dex=line.lastIndexOf(':');
String str=line.substring(dex+2,dex+8 );
//System.out.println("8888:"+str);
return (str);