簡訊傳送api示例
阿新 • • 發佈:2019-01-09
首先說一下簡訊驗證的使用場景。
1、與賬戶資訊相關的操作,如修改支付寶賬號等
2、與資金流轉相關的操作,如提現等
只有在以上兩種場景下,簡訊驗證才會起到作用。即提交相關操作的時候,附帶一個驗證碼。
在本專案中,使用的簡訊平臺是雲片網。本文會舉一個例子來演示簡訊:
流程
1、雲片網上的簡訊是通過一個http請求觸發的。
// 通用傳送介面的http地址
private static String URI_SEND_SMS = "http://yunpian.com/v1/sms/send.json";
//模板傳送介面的http地址
private static String URI_TPL_SEND_SMS = "https://sms.yunpian.com/v1/sms/tpl_send.json";
//傳送語音驗證碼介面的http地址
private static String URI_SEND_VOICE = "https://voice.yunpian.com/v1/voice/send.json";
/**
* 基於HttpClient 4.3的通用POST方法
*
* @param url
* 提交的URL
* @param paramsMap
* 提交<引數,值>Map
* @return 提交響應
*/
public static String post(String url, Map<String, String> paramsMap) {
StringBuilder str=new StringBuilder("呼叫傳送簡訊外部介面:post-->");
CloseableHttpClient client = HttpClients.createDefault();
String responseText = "" ;
CloseableHttpResponse response = null;
try {
HttpPost method = new HttpPost(url);
if (paramsMap != null) {
List<NameValuePair> paramList = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> param : paramsMap.entrySet()) {
NameValuePair pair = new BasicNameValuePair(param.getKey(),
param.getValue());
paramList.add(pair);
}
method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
}
response = client.execute(method);
HttpEntity entity = response.getEntity();
if (entity != null) {
responseText = EntityUtils.toString(entity);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
response.close();
} catch (Exception e) {
e.printStackTrace();
}
}
str.append("responseText:").append(responseText).append("_");
System.out.println("responseText:"+responseText);
return responseText;
}
1、在雲片網上註冊一個帳號,購買相應的服務。
2、設定傳送簡訊的模版
模版的組成部分:【簽名】+簡訊正文
先要申請簽名。簽名通過之後,在使用該簽名新增模版。
格式如:【XXX】親愛的使用者,您的註冊碼是#code#。如非本人操作,請忽略此簡訊。
3、三種不同的方式
1)傳送模版的時候,可以以text文字傳送,即傳送的時候text與模版進行匹配
/**
* 使用模版匹配方式發簡訊
*
* @param apikey apikey(來自雲片網的賬戶資訊)
* @param text 簡訊內容
* @param mobile 接受的手機號
* @return json格式字串
* @throws IOException
*/
public static String sendSms(String apikey, String text, String mobile)
throws IOException {
Map<String, String> params = new HashMap<String, String>();
//簡訊驗證碼——模糊匹配方式
params.put("apikey", apikey);
params.put("text", text);
params.put("mobile", mobile);
return post(URI_SEND_SMS, params);
}
2)另一種方式是,直接指定模版id進行傳送
/**
* 指定模版發簡訊
*
* @param apikey apikey(來自雲片網的賬戶資訊)
* @param tpl_id 模版id
* @param tpl_value 模版中的萬用字元
* @param mobile 接受的手機號
* @return json格式字串
* @throws IOException
*/
public static String sendSms(String apikey, String text, String mobile)
throws IOException {
Map<String, String> params = new HashMap<String, String>();
//簡訊驗證碼——使用模版
params.put("apikey", apikey);
params.put("tpl_id", "994567"); //模版id
params.put("tpl_value", "#code#=1234");
params.put("mobile", mobile);
return post(URI_TPL_SEND_SMS, params);
}
3)使用語音播報的方式
/**
* 使用語音播報的方式
*
* @param apikey apikey(來自雲片網的賬戶資訊)
* @param code 驗證碼
* @param mobile 接受的手機號
* @return json格式字串
* @throws IOException
*/
public static String sendSms(String apikey, String text, String mobile)
throws IOException {
Map<String, String> params = new HashMap<String, String>();
//使用語音播報
params.put("apikey", apikey);
params.put("mobile", mobile);
params.put("code", "456789");
return post(URI_SEND_VOICE, params);
}
呼叫傳送簡訊:
public static void main(String[] args) throws IOException {
String apikey = "雲片網上的apikey"
String text = "【簽名】您的驗證碼是";
String code = "";
Random random = new Random();
for (int i = 0; i < 6; i++) {
code += random.nextInt(10);
}
text = text + code;
sendSms(apikey, text, "187XXXX5771");//手機號
System.out.println("code:" + code);
}
簡訊傳送,算是一塊成熟的技術。所以肯定有第三方程式已經提供了。我們需要做的就是找到這些程式,把他們提供的使用到我們的專案中。