验证码,语音验证码
//发送语音验证码接口的http地址
private static String URI_SEND_VOICE = “https://voice.yunpian.com/v1/voice/send.json”;
//智能匹配模版发送接口的http地址
private static String URI_SEND_SMS = “https://sms.yunpian.com/v1/sms/send.json”;
/**
* 通过接口发送语音验证码
* @param apikey apikey
* @param mobile 接收的手机号
* @param code 验证码
* @return
*/
public static String sendVoice(String apikey, String mobile, String code) {
Map<String, String> params = new HashMap<String, String>();
params.put(“apikey”, apikey);
params.put(“mobile”, mobile);
params.put(“code”, code);
return post(URI_SEND_VOICE, params);
}
public static boolean smsSendVoice(String mobile, String code){
boolean flag = false;
try {
String result = SmsUtil.sendVoice(AppConfig.apiKey, mobile, code);
logger.info(result);
JSONObject obj = JSONObject.fromObject(result);
int respCode = (Integer)obj.get(“code”);
String msg = obj.get(“msg”)==null?”检测您发送短信频率过高,请您稍后再试”:(String)obj.get(“msg”);
logger.info(“code: ” + respCode + “; msg: ” + msg );
if(respCode == 0 && null != msg && “OK”.equals(msg)) {
flag = true;
}} catch (Exception e) {
// TODO: handle exception
logger.error(“SmsUtil.smsSendVoice IOException”, e);
}
return flag;
}
//发送密码接口
public static boolean SmsApiForPwd(String userPhone, String pwd) {
StringBuffer textBuffer = new StringBuffer();
textBuffer.append(“【畅的充电】您的密码是”).append(pwd).append(“,”).append(“请您妥善保管”);
String text = textBuffer.toString();
boolean flag = false;
try {
String mobile = URLEncoder.encode(userPhone, ENCODING);
String result = SmsUtil.sendSms(AppConfig.apiKey, text, mobile);
logger.info(result);
JSONObject obj = JSONObject.fromObject(result);
int respCode = (Integer)obj.get(“code”);
String msg = (String)obj.get(“msg”);
logger.info(“code: ” + respCode + “; msg: ” + msg );
if(respCode == 0 && null != msg && “OK”.equals(msg)) {
flag = true;
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
logger.error(“SmsUtil.SmsApi UnsupportedEncodingException”, e1);
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error(“SmsUtil.SmsApi IOException”, e);
}
return flag;
}
//发送验证码接口
public static boolean SmsApi(String userPhone, String code) {
StringBuffer textBuffer = new StringBuffer();
textBuffer.append(“【畅的充电】您的验证码是”).append(code).append(“,”).append(AppConfig.expire_date).append(“分钟内有效。若非本人操作请忽略此消息。”);
String text = textBuffer.toString();
boolean flag = false;
try {
String mobile = URLEncoder.encode(userPhone, ENCODING);
String result = SmsUtil.sendSms(AppConfig.apiKey, text, mobile);
logger.info(result);
JSONObject obj = JSONObject.fromObject(result);
int respCode = (Integer)obj.get(“code”);
String msg = obj.get(“msg”)==null?”检测您发送短信频率过高,请您稍后再试”:(String)obj.get(“msg”);
logger.info(“code: ” + respCode + “; msg: ” + msg );
if(respCode == 0 && null != msg && “OK”.equals(msg)) {
flag = true;
}
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
logger.error(“SmsUtil.SmsApi UnsupportedEncodingException”, e1);
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error(“SmsUtil.SmsApi IOException”, e);
}
return flag;
}
/**
* 智能匹配模版接口发短信
*
* @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);
}
/**
* 基于HttpClient 4.3的通用POST方法
*
* @param url 提交的URL
* @param paramsMap 提交<参数,值>Map
* @return 提交响应
*/
public static String post(String url, Map<String, String> paramsMap) {
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();
}
}
return responseText;
}
public String getSecurityCode() {
String securityCode = “”;
for(int i=0 ; i<4 ; i++) {
securityCode += UmsConfig.intArray[(int) Math.floor(Math.random()*10)];
}
return securityCode;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println(3);
SmsUtil.SmsApi(“153********”, “999999”);
// String str = “{\’code\’:0,\’msg\’:\’OK\’,\’result\’:{\’count\’:1,\’fee\’:0.055,\’sid\’:5033613460}}”;
// JSONObject obj = JSONObject.fromObject(str);
// System.out.println((Integer)obj.get(“code”));
// System.out.println((String)obj.get(“msg”));
}