很多用户注册这个模块都有手机验证这个功能,接下来使用“互亿无线”短信验证码接口来实现发送手机验证码功能。
1、首先,上互亿无线注册一个账号,http://www.ihuyi.com/,注册后你可以看到你的账号参数信息。
2、后台需要参数:
(1)在配置文件中的信息
spring.profiles.SMB_SRVUSERNAME=WWW
spring.profiles.SMB_SRVPASSWORD=123456
spring.profiles.SMB_SRVIP=127.0.0.1
spring.profiles.SMB_FILELOCATION=tobo
spring.profiles.SMB_PICLOCATION=http://域名?account=userId&password=123456
spring.profiles.SEND_SMS_URL=发送地址
(2)短信接口
String postUrl = SEND_SMS_URL + "&mobile=" + 手机号 + "&content=" + 内容;
public static String sendSMS(String postUrl) {
String result = "";
try {
//发送POST请求
URL url = new URL(postUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setUseCaches(false);
conn.setDoOutput(true);
//conn.setRequestProperty("Content-Length", "" + postData.length());
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
//out.write(postData);
out.flush();
out.close();
//获取响应状态
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~connect failed!");
System.out.println("connect failed!");
return "";
}
//获取响应内容体
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = in.readLine()) != null) {
result += line + "\n";
}
in.close();
logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + postUrl);
logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" + result);
return result;
} catch (IOException e) {
e.printStackTrace(System.out);
}
return result;
}