首先一睁,很多第三方短信驗(yàn)證碼都可以冈闭,為什么我要選擇mob的呢?
因?yàn)閙ob的短信驗(yàn)證碼是完全免費(fèi)
并且支持IOS册招,Android岔激,Unity3d,Cocos2d-X的集成
Mob的官網(wǎng):http://www.mob.com/
Mob官方文檔:http://www.mob.com/api/documentList
那Java怎么用呢是掰?
其實(shí)MOB開發(fā)文檔中有詳細(xì)的介紹虑鼎。
使用MOB不需要導(dǎo)入任何依賴,只需要把接口文檔Java語言的代碼拷貝下來
主要有兩個(gè)方法:
1.requestData()這個(gè)是發(fā)送驗(yàn)證請(qǐng)求的方法
/**
* 發(fā)起https 請(qǐng)求
* @param address
* @param params
* @return
*/
public static String requestData(String address ,String params){
HttpURLConnection conn = null;
try {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){
public X509Certificate[] getAcceptedIssuers(){return null;}
public void checkClientTrusted(X509Certificate[] certs, String authType){}
public void checkServerTrusted(X509Certificate[] certs, String authType){}
}};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
//ip host verify
HostnameVerifier hv = new HostnameVerifier() {
public boolean verify(String urlHostName, SSLSession session) {
return urlHostName.equals(session.getPeerHost());
}
};
//set ip host verify
HttpsURLConnection.setDefaultHostnameVerifier(hv);
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
URL url = new URL(address);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");// POST
conn.setConnectTimeout(3000);
conn.setReadTimeout(3000);
// set params ;post params
if (params!=null) {
conn.setDoOutput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(params.getBytes(Charset.forName("UTF-8")));
out.flush();
out.close();
}
conn.connect();
//get result
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
String result = parsRtn(conn.getInputStream());
return result;
} else {
System.out.println(conn.getResponseCode() + " "+ conn.getResponseMessage());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return null;
}
返回的result中有返回值和狀態(tài)描述
2.parsRtn()方法冀惭,這個(gè)方法是獲取返回?cái)?shù)據(jù)
在MOB的JAVA樣例程序: https://github.com/tian-github/Mob_SmsSpi中可以看到
/**
* 獲取返回?cái)?shù)據(jù)
* @param is
* @return
* @throws IOException
*/
private static String parsRtn(InputStream is) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String line = null;
boolean first = true;
while ((line = reader.readLine()) != null) {
if(first){
first = false;
}else{
buffer.append("\n");
}
buffer.append(line);
}
return buffer.toString();
}
也是直接拷過來震叙,這兩個(gè)方法我都寫成靜態(tài)static
了掀鹅,方便調(diào)用
發(fā)送請(qǐng)求
String result=方法所在類.requestData(verifyUrl,params);
靜態(tài)方法直接通過類.方法來調(diào)用
在官方的文檔中
verifyUrl
是https://webapi.sms.mob.com/sms/verify
,請(qǐng)求的 URL這個(gè)是固定的
params
包括了以下參數(shù)
params的請(qǐng)求格式是
appkey=xxxx&phone=xxxx&zone=86&&code=xxxx
result
是一個(gè)JSON
格式的字符串,如果想要提取出里面的狀態(tài)碼媒楼,可以使用fastjson
JSON.parseObject(result).get("status").toString()
乐尊,這樣就取出了String
類型的狀態(tài)碼根據(jù)不同的狀態(tài)碼就可以判斷驗(yàn)證是否通過