wangxiaopei

1.设计:用户填写手机号,点击获取验证码按钮,controller层获得所要发送验证码的手机号,随机生成六位数的验证码。调用发送短信接口url向此手机号发送验证码。注意:下面的代码采用的是云信使的短信验证码接口,不同的验证码更换不同的url格式,即可。
`

@RequestMapping(value = "/sendValidate")
public void sendValidate(HttpServletRequest request,HttpServletResponse response) throws MalformedURLException {
    String vcode = "";
    String phonenumber = request.getParameter("phonenumber");
    URL url;
    String str = null;
    JSONObject json = new JSONObject();
    //生成验证码 
    for (int i = 0; i < 6; i++) {
        vcode = vcode + (int) (Math.random() * 9);
    }

    User us = userService.getUserMessage(phonenumber);
    if (us != null) {
        json.put("status", -2);// 电话号码已被注册过
        try {
            ResponseUtil.write(response, json.toString());
        } catch (Exception e1) {
        }
        return;
    }
    try {
         url = new URL(
         "http://api.sms.cn/sms/?ac=send&uid=用户名&pwd=你注册时md5加密的密码&template=100006&mobile="+phonenumber+"&content={code:"+vcode+"}");
         URLConnection connection = url.openConnection();
        connection.setDoOutput(true);      
        DataInputStream read= new
    DataInputStream(connection.getInputStream());
        str = read.readUTF();
    } catch (Exception e) {
        json.put("status", -1);// 服务器端错误,例如没网了
        try {
            ResponseUtil.write(response, json.toString());
        } catch (Exception e1) {
        }
        return;
    }
    if (str != null && str.substring(6, 9).equals("100") == true) {
        json.put("status", 1);// 验证码发送成功
    } else
        json.put("status", 0);// 验证码发送失败
    try {
        ResponseUtil.write(response, json.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }

}`

分类:

技术点:

相关文章:

  • 2022-01-01
  • 2021-05-14
  • 2022-12-23
  • 2021-07-13
  • 2021-10-03
  • 2021-07-04
  • 2021-08-25
  • 2021-12-03
猜你喜欢
  • 2022-01-07
  • 2021-11-17
  • 2021-11-16
  • 2021-11-20
  • 2021-12-22
  • 2021-04-05
  • 2021-09-01
相关资源
相似解决方案