2018-10-18【插件分析】java接入短信验证码功能

2018-10-18  本文已影响0人  胖胖王321

今天公司提出一个需求,要在现有项目上收集注册用户的真实手机号,由于之前没有接触过这一块,只能寻求度娘的帮助,经过一天的努力,终于完成了,现整理记录下已备查阅。

1 解决方案:在注册时要求用户进行手机验证。

2 寻找短信供应商:由于对这一块不是太懂,大学同学推荐一家他们公司在用的给我。

3 代码实现

  首先到互亿无线短信平台注册一个帐号,并登录到用户中心,选验证码模块下载接口文档,文档下载下来是一个压缩包,我们项目是用java的jsp开发的,直接找到目录DEMO/JSP

  提示:开始之前先看一下官方对接说明文档

  第一步 把前端html代码整合到注册页面

  html 代码:

<form action="reg.jsp" method="post" name="formUser" onSubmit="return register();">

<table width="100%"  border="0" align="left" cellpadding="5" cellspacing="3">

<tr>

<td align="right">手机<td>

<input id="mobile" name="mobile" type="text" size="25" class="inputBg" /><span style="color:#FF0000"> *</span>

        </tr>

  <tr>

    <td align="right">验证码</td>

    <td>

    <input type="text" name="gd_code" class="inputBg" size="25" id="gd_code" onkeyup="value=value.replace(/[^\d]/g,'')" placeholder="请输入正确的验证码">

    <span>&nbsp;<img src="code.jsp" onClick="javascript:this.src=this.src+'?date='+Date();" ></span>

    </td>

        </tr>

<tr>

<td align="right">手机验证码</td>

<td align="left">

<input type="text"  name="mobile_code" class="inputBg" size="25" />

<input id="zphone" type="button" value=" 获取手机验证码 " style="width: 120px"  onClick="get_mobile_code()">

</td>

</tr>

</table>

</form>

javascript代码

<script language="javascript">

function get_mobile_code(){

        $.post('sms.jsp', {mobile:jQuery.trim($('#mobile').val()),send_code:$("#gd_code").val()}, function(msg) {

            alert(jQuery.trim(unescape(msg)));

if(msg=='提交成功'){

RemainTime();

}else{

location.reload();

}

        });

};

var iTime = 59;

var Account;

function RemainTime(){

document.getElementById('zphone').disabled = true;

var iSecond,sSecond="",sTime="";

if (iTime >= 0){

iSecond = parseInt(iTime%60);

iMinute = parseInt(iTime/60)

if (iSecond >= 0){

if(iMinute>0){

sSecond = iMinute + "分" + iSecond + "秒";

}else{

sSecond = iSecond + "秒";

}

}

sTime=sSecond;

if(iTime==0){

clearTimeout(Account);

sTime='获取手机验证码';

iTime = 59;

document.getElementById('zphone').disabled = false;

}else{

Account = setTimeout("RemainTime()",1000);

iTime=iTime-1;

}

}else{

sTime='没有倒计时';

}

document.getElementById('zphone').value = sTime;

}

</script>

第一步 把后端jsp代码整合到程序逻辑中。

注意:把code.jsp sms.jsp这两个文件要上传到项目路径下

使用 HttpURLConnection请求短信接口就行了

String postUrl = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

int mobile_code = (int)((Math.random()*9+1)*100000); //获取随机数

String account = "用户名"; //查看用户名是登录用户中心->验证码短信->产品总览->APIID

String password = "密码";  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY

String mobile = request.getParameter("mobile");

String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");

String send_code = request.getParameter("send_code");

String session_code = (String)session.getAttribute("randStr");

// 此处验证码仅限于纯数字比对

if (Integer.parseInt(send_code) != Integer.valueOf(session_code)) {

String message = new String("请输入正确的验证码");

out.println(message);

return;

}

try {

URL url = new URL(postUrl);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

connection.setDoOutput(true);//允许连接提交信息

connection.setRequestMethod("POST");//网页提交方式“GET”、“POST”

connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

connection.setRequestProperty("Connection", "Keep-Alive");

StringBuffer sb = new StringBuffer();

sb.append("account="+account);

sb.append("&password="+password);

sb.append("&mobile="+mobile);

sb.append("&content="+content);

OutputStream os = connection.getOutputStream();

os.write(sb.toString().getBytes());

os.close();

String line, result = "";

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));

while ((line = in.readLine()) != null) {

result += line + "\n";

}

in.close();

out.println(result);

} catch (IOException e) {

e.printStackTrace(System.out);

}

上一篇下一篇

猜你喜欢

热点阅读