网易云短信接口 ---- 短信验证码

这两天写了一个关于网易云的短信验证码功能,然后jar包以及代码:链接:https://pan.baidu.com/s/1H8FUgfkqNJFNeebByCh_jw
提取码:yvy3
复制这段内容后打开百度网盘手机App,操作更方便哦(如果是用maven的话,就直接使用pom.xml;如果不是的话里面也会有lib文件夹,直接用里面的jar包也是可以的)

计算CheckSum:

package action;

import java.security.MessageDigest;
/**
 * Created by admin on 2017/9/22.
 */
public class CheckSumBuilder {

        // 计算并获取CheckSum sha1 不是 shal 1 l
        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 action;

import com.alibaba.fastjson.JSON;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 短信发送工具类
 * @author Administrator
 *
 */
public class MobileMessageSend {
    //发送验证码的请求路径URL
    private static final String SERVER_URL="https://api.netease.im/sms/sendcode.action";
    //网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
    private static final String APP_KEY="你自己的";
    //网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret
    private static final String APP_SECRET="你自己的";
    //随机数
    private static final String NONCE="123456";
    //短信模板ID
    private static final String TEMPLATEID="你自己的";
    //手机号
//    private static final String MOBILE="接收者,当然这里注释了。";
    //验证码长度,范围4~10,默认为4
    private static final String CODELEN="6";

     public static void sendMsg(String phone) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(SERVER_URL);

//      CheckSum的计算
        String curTime=String.valueOf((new Date().getTime()/1000L));
        String checkSum= CheckSumBuilder.getCheckSum(APP_SECRET,NONCE,curTime);

        //设置请求的header
        post.addHeader("AppKey",APP_KEY);
        post.addHeader("Nonce",NONCE);
        post.addHeader("CurTime",curTime);
        post.addHeader("CheckSum",checkSum);
        post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");

        //设置请求参数
        List nameValuePairs =new ArrayList<>();
        nameValuePairs.add(new BasicNameValuePair("mobile",phone));
        nameValuePairs.add(new BasicNameValuePair("templateid", TEMPLATEID));
        nameValuePairs.add(new BasicNameValuePair("codeLen", CODELEN));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));

        //执行请求
        HttpResponse response=httpclient.execute(post);
        String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");

        //获取发送状态码
        String code= JSON.parseObject(responseEntity).getString("code");
        if (code.equals("200")){
            System.out.println("发送成功!");
            return;
        }
        System.out.println("发送失败!");
    }
}

获取发送的验证码:

String sms= JSON.parseObject(responseEntity).getString("obj");

main方法:

package action;

import java.io.IOException;

/**
 * Created by admin on 2017/9/22.
 */
public class SendMsg {
    public static void main(String[] args) throws IOException {
        String mobileNumber = "接收验证码的手机号码";
        MobileMessageSend.sendMsg(mobileNumer);   
	}
}

然后附带一个状态码网址:http://dev.netease.im/docs/product/短信/短信状态码

然后补一波 APP_KEY 和 APP_SECRET:
网易云短信接口 ---- 短信验证码_第1张图片
点击 App Key 管理 后往下拉
网易云短信接口 ---- 短信验证码_第2张图片

然后下一篇会有如何在页面实现点击发送短信,并实现定时器功能

有不对的地方还请多多指教!

你可能感兴趣的:(网易云短信)