java实现手机发送验证码

来源:互联网 发布:达索软件dassault 编辑:程序博客网 时间:2024/05/22 00:06

运行本程序首先的代入三个jar包:

commons-codec-1.4

commons-httpclient-3.1

commons-logging-1.1.1


注册所需的账号到中国网建去注册


短信发送后返回值说 明-1没有该用户账户-2密钥不正确(不是用户密码)-3短信数量不足-11该用户被禁用-14短信内容出现非法字符-41手机号码为空-42短信内容为空大于0短信发送数量

《前台jsp代码》

            

 <form class="bs-example bs-example-form" role="form" method="post">                    <input type="text" class="form-control" id="phone" name="phone" placeholder="请输入手机号"><br/>                    <input type="text" class="form-control" id="code" name="code" placeholder="请输入验证码">                    <input id="btnSendCode" type="button" value="获取验证码" onclick="sendMessage()" />                                   <br/><br/><br/><br/><br/>                    <button type="button" class="btn btn-primary btn-lg " onclick="register()">立即注册</button>         </form>               <script type="text/javascript">        var phoneCode = '<%=request.getAttribute("phoneCode")%>';                var InterValObj; //timer变量,控制时间          var count = 120; //间隔函数,1秒执行          var curCount;//当前剩余秒数          var code = ""; //验证码          var codeLength = 6;//验证码长度                 //获取短信验证码        function sendMessage() {                        curCount = count;              var phone = $("#phone").val();                          if(phone != ""){                // 产生验证码                  for ( var i = 0; i < codeLength; i++) {                      code += parseInt(Math.random() * 9).toString();                  }                  //设置button效果,开始计时                  $("#btnSendCode").attr("disabled", "true");                  $("#btnSendCode").val("请在" + curCount + "秒内输入验证码");                  InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次            }            /* alert(phone);            alert(code); */            //向后台发送处理数据            $.ajax({                url:"<%=request.getContextPath() %>/user/getCode.controller",                type:"post",                data:"phone=" + phone +"&code=" + code,                dataType:"text",            });                    }        //注册        function register() {            var code = $("#code").val();            var password = $("#password").val();            var regPassword = $("#regPassword").val();                        $.ajax({                url:"<%=request.getContextPath() %>/user/register.controller",                type:"post",                data:"phone=" + phone +"&code=" + code +"&password=" + password +"&regPassword=" +regPassword+"&phoneCode="+phoneCode,                dataType:"json",                success:function(obj){                    alert(obj);                    if(obj){                        location.href = "<%=request.getContextPath()%>/user/toSuccess.controller";                    }                }            });        }                        //timer处理函数          function SetRemainTime() {              if (curCount == 0) {                                  window.clearInterval(InterValObj);//停止计时器                  $("#btnSendCode").removeAttr("disabled");//启用按钮                  $("#btnSendCode").val("重新发送验证码");                  code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效                  }              else {                  curCount--;                  $("#btnSendCode").val("请在" + curCount + "秒内输入验证码");              }          }              </script>

《后台java代码》

   

/**     * @return 获得验证码并给手机发送     * @throws IOException     * @throws HttpException     */    @RequestMapping("getCode")    public String getCode(HttpServletRequest request, HttpServletResponse response) throws Exception {        // 获得要发送的手机号和验证码        String phone = request.getParameter("phone");        String code = request.getParameter("code");               // 将验证码、手机号存到session中        request.getSession().setAttribute("code", code);        request.getSession().setAttribute("phone", phone);        HttpClient client = new HttpClient();        PostMethod post = new PostMethod("http://gbk.sms.webchinese.cn");        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// 在头文件中设置转码        NameValuePair[] data = { new NameValuePair("Uid", "wr"), // 注册的用户名                new NameValuePair("Key", "e02bdc41003f2ded4832"), // 注册成功后,登录网站使用的密钥                new NameValuePair("smsMob", phone), // 手机号码                new NameValuePair("smsText", "您的验证码为:" + code + "【此为必填项】") };// 设置短信内容        post.setRequestBody(data);        client.executeMethod(post);        Header[] headers = post.getResponseHeaders();        int statusCode = post.getStatusCode();        System.out.println("statusCode:" + statusCode);        for (Header h : headers) {            System.out.println(h.toString());        }        String result = new String(post.getResponseBodyAsString().getBytes("gbk"));        System.out.println(result);        post.releaseConnection();        return null;    }





0 0
原创粉丝点击