短信验证码

来源:互联网 发布:网络储备人才招聘 编辑:程序博客网 时间:2024/06/05 02:48

                                                    短信验证码

我用的是网易云信平台的接口。其他的平台也看过大致差不多:

这是从平台的开发手册找的java版的案例,将其中的一些参数改下就可以了:

package com.yinhe.utils;import java.security.MessageDigest;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.apache.http.HttpRequest;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;/** * 发送验证码 * @author liuxuanlin * */public class MessageUtils {    //发送验证码的请求路径URL    private static final String            SERVER_URL="https://api.netease.im/sms/sendcode.action";    //网易云信分配的账号,请替换你在管理后台应用下申请的Appkey    private static final String             APP_KEY="9c8544*************c6a80";    //网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret    private static final String APP_SECRET="03d****ade";    //随机数    private static final String NONCE="0123456789";    //短信模板ID    private static final String TEMPLATEID="3119013";       //验证码长度,范围4~10,默认为4    private static final String CODELEN="6";    public static String sendMessage(String phone) throws Exception {     //手机号        String MOBILE=phone;        DefaultHttpClient httpClient = new DefaultHttpClient();        HttpPost httpPost = new HttpPost(SERVER_URL);        String curTime = String.valueOf((new Date()).getTime() / 1000L);        /*         * 参考计算CheckSum的java代码,在上述文档的参数列表中,有CheckSum的计算文档示例         */        String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime);        // 设置请求的header        httpPost.addHeader("AppKey", APP_KEY);        httpPost.addHeader("Nonce", NONCE);        httpPost.addHeader("CurTime", curTime);        httpPost.addHeader("CheckSum", checkSum);        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");        // 设置请求的的参数,requestBody参数        List nvps = new ArrayList();        /*         * 1.如果是模板短信,请注意参数mobile是有s的,详细参数配置请参考“发送模板短信文档”         * 2.参数格式是jsonArray的格式,例如 "['13888888888','13666666666']"         * 3.params是根据你模板里面有几个参数,那里面的参数也是jsonArray格式         */        nvps.add(new BasicNameValuePair("templateid", TEMPLATEID));        nvps.add(new BasicNameValuePair("mobile", MOBILE));        nvps.add(new BasicNameValuePair("codeLen", CODELEN));        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));        // 执行请求        HttpResponse response = httpClient.execute(httpPost);        /*         * 1.打印执行结果,打印结果一般会200、315、403、404、413、414、500         * 2.具体的code有问题的可以参考官网的Code状态表         */        return EntityUtils.toString(response.getEntity(), "utf-8");      }    }class CheckSumBuilder {    // 计算并获取CheckSum    public static String getCheckSum(String appSecret, String nonce, String curTime) {        return encode("sha1", appSecret + nonce + curTime);    }    // 计算并获取md5值    public static String getMD5(String requestBody) {        return encode("md5",requestBody);    }    private static String encode(String algorithm, String value) {        if (value == null) {            return null;        }        try {            MessageDigest messageDigest                    = MessageDigest.getInstance(algorithm);            messageDigest.update(value.getBytes());            return getFormattedText(messageDigest.digest());        } catch (Exception e) {            throw new RuntimeException(e);        }    }    private static String getFormattedText(byte[] bytes) {        int len = bytes.length;        StringBuilder buf = new StringBuilder(len * 2);        for (int j = 0; j < len; j++) {            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);        }        return buf.toString();    }    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };}package com.yinhe.web.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.yinhe.utils.MessageUtils;public class MessageServlet extends BaseServlet {    public void hqyzm(HttpServletRequest req,HttpServletResponse resp) throws Exception {//MessageUtils.sendMsg(req.getParameter("phone"), req.getParameter("yanz"));    String phone=req.getParameter("phone");    String  me=MessageUtils.sendMessage(phone);    System.out.println(me);    JSONObject jsonObject= (JSONObject) JSON.parse(me);    String obj = jsonObject.getString("obj");      req.getSession().setAttribute("code", obj);    System.out.println(obj);    resp.setContentType("text/html;charset=UTF-8");    boolean result=false;if(phone!=null){result=true;}String jsonString = "{'result':"+result+"}";resp.getWriter().println(jsonString);}public void yz(HttpServletRequest req,HttpServletResponse resp) {  String  p=req.getParameter("yanz");  System.out.println(p);  System.out.println(req.getSession().getAttribute("code"));    if(p.equals(req.getSession().getAttribute("code"))){    try {    resp.setContentType("text/html;charset=UTF-8");resp.getWriter().println("");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}    }else {    try {    resp.setContentType("text/html;charset=UTF-8");resp.getWriter().println("");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>          My JSP 'duanxin.jsp' starting page        label.error {   COLOR: red;}    
请输入手机号:
验     证     码:  


原创粉丝点击