全国通用标准化HTTP短信验证码接口,接入从未如此简单

熟练掌握接口对接是一个程序猿的基本功,短信验证码接口作为企业商用最常见也是用的最多的一个接口,能够快速的接入当然是可以省下不少的时间啦!下面给大家介绍一个简单快速的接入方法啦!

 

验证码的前端代码很简单,就是发送验证码和校验验证码两个ajax请求。强烈建议加一个图形验证码,防止短信被刷,实现代码如下。



    
    
    
    
    
    
    
    
    


 

后端代码稍微复杂那么一点点,一共分三步:

 

  1. 接收生成验证码请求,生成验证码存入session中;
  2. 接收校验验证码的请求,将存入session的验证码去除;
  3. 与前端输入的验证码进行比对,比对一致则通过并进行视图转发,比对不一致则告知前端错误原因。

 

实现代码如下:

HttpSession session = req.getSession();
    // TODO 验证码有效时间
    session.setMaxInactiveInterval(600);
    try {
      Integer num = RandNumber.getNum();
      // TODO 发送验证码通道
      Sendsms.Send(num, phone);
      session.setAttribute(phone, num);
      return R.ok();
    } catch (Exception e) {
      e.printStackTrace();
      logger.error(e.getMessage());
      return R.error("fasle");
    }
import java.io.Exception;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
 
public class Sendsms {
 
  private static String Url = "https://vip.veesing.com/smsApi/verifyCode";
 
  // 发送短信验证码
  public static void Send(Integer num, String mobile) {
    try {
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(Url);
    client.getParams().setContentCharset("UTF-8");
    method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
    NameValuePair[] data = { 
        new NameValuePair("appId", "*********"),
        new NameValuePair("appKey", "**********"), 
        new NameValuePair("templateId", "*******"), 
        new NameValuePair("mobile", "*******"),
        new NameValuePair("variables", "*******") 
        };
    method.setRequestBody(data);
    client.executeMethod(method);
    String submitResult = method.getResponseBodyAsString();
    System.out.println(submitResult);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
}
HttpSession session = req.getSession();
    String yzm = String.valueOf(session.getAttribute(username));
    logger.info(yzm);
    if (yzm == null) {
      return R.error("验证码错误");
    }
    if (yzm != null && !verifycode.equals(yzm)) {
      return R.error("验证码错误");
    }

对了,还有一点,很多短信平台都设置的有appId和appKey,查看方法如下图

全国通用标准化HTTP短信验证码接口,接入从未如此简单_第1张图片

当然,我之所以选择以上这个平台主要是有免费测试条数,接口也是标准化的HTTP接口,接入很方便,价格方面也适中。很多短信平台都有免费的测试,大家也可以找找看,最后给大家附上短信验证码demo。

package com.veesing.test; 

import java.io.IOException; 

import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.HttpException; 
import org.apache.commons.httpclient.NameValuePair; 
import org.apache.commons.httpclient.methods.PostMethod; 
import com.alibaba.fastjson.JSONObject; 
import com.veesing.utils.Config; 

/** 
 * 短信验证码 
 * @author MWH 
 * 
 */ 
public class SmsCodeTest { 
    public static void main(String[] args) { 
        // 获取连接 
        HttpClient client = new HttpClient(); 
        // 短信验证码API接口地址 
        PostMethod method = new PostMethod("https://vip.veesing.com/smsApi/verifyCode"); 
        // 设置编码 
        client.getParams().setContentCharset("UTF-8"); 
        method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=utf-8"); 
        // 手机号码,一次只能提交一个手机号码 
        String phone = "15080929435"; 
        //模板ID(如没有模板ID请先在平台上新增并提交验证码模板,审核通过即可使用) 
        String templateId = "36"; 
        // 验证码变量(随机数) 
        Integer num = (int)((Math.random()*9+1)*1000); 
        String variables = num.toString(); 
        System.out.println("验证码是:"+variables); 
        // 拼接参数 
        NameValuePair[] data = {  
                new NameValuePair("appId", Config.appid),  
                new NameValuePair("appKey", Config.appkey), 
                new NameValuePair("phone", phone),  
                new NameValuePair("templateId", templateId),  
                new NameValuePair("variables", variables) }; 
        method.setRequestBody(data); 
        try { 
            client.executeMethod(method); 
            String result = method.getResponseBodyAsString(); 
            // 返回结果 
            System.out.println(result); 
            JSONObject jsonObject = JSONObject.parseObject(result); 
            // 返回2000则发送成功(逻辑操作请根据接口文档返回参数自行判断) 
            if (jsonObject.get("returnStatus").equals("2000")) { 
                System.out.println("成功!"); 
            } else { 
                System.out.println("失败!"); 
            } 
            // 释放连接 
            method.setRequestHeader("Connection", "close");   
            method.releaseConnection(); 
        } catch (HttpException e) { 
            e.printStackTrace(); 
        } catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 
} 

 

你可能感兴趣的:(短信验证码接口)