• 网易云信(验证码短信接口接入)


    准备工具类:chekSum

    package net.tiantianup.commons.utils;
    
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    /**
     * Created by LV on 2016/4/15 0015.
     * Email:LvLoveYuForever@gmail.com
     */
    public class CheckSumBuilder {
    
        //计算并获取checkSum
        public static String getCheckSum(String appSecret,String nonce,String curTime){
            return encode("SHA",appSecret+nonce+curTime);
        }
    
        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 sb=new StringBuilder(len*2);
            for(int $i=0;$i<len;$i++){
                sb.append(HEX_DIGITS[(bytes[$i]>>4)&0x0f]);
                sb.append(HEX_DIGITS[bytes[$i]&0x0f]);
            }
            return sb.toString();
        }
    
        private static final char[] HEX_DIGITS={'0','1','2','3','4','5','6',
                '7','8','9','a','b','c','d','e','f'};
    
    }

    封装的短信发送工具类:

    package net.tiantianup.commons.utils;
    
    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;
    
    /**
     * 短信工具类
     * Created by LV on 2016/4/15 0015.
     * Email:LvLoveYuForever@gmail.com
     */
    public class MobileMessageSend {
        private static final String SERVER_URL="https://api.netease.im/sms/sendtemplate.action";//请求的URL
        private static final String APP_KEY="填入账号";//账号
        private static final String APP_SECRET="填入密码";//密码
        private static final String MOULD_ID="填入设置的模板ID";//模板ID
        private static final String NONCE="123456";
    
        public static int sendMsg(String phone,String msg) throws IOException {
    
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost post = new HttpPost(SERVER_URL);
    
            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<NameValuePair> nameValuePairs =new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("templateid",MOULD_ID));
            nameValuePairs.add(new BasicNameValuePair("mobiles","['"+phone+"']"));
            nameValuePairs.add(new BasicNameValuePair("params","['"+msg+"']"));
    
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
    
            //执行请求
            HttpResponse response=httpclient.execute(post);
    
            String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");
    
            //判断是否发送成功,发送成功返回true
            String code= JSON.parseObject(responseEntity).getString("code");
            if (code.equals("200"))
                {return 0;}
    
            return 500;
        }
    }
  • 相关阅读:
    Java线程池
    代理模式
    Bean的加载
    Spring的启动流程
    MFC编程入门之二十七(常用控件:图片控件PictureControl)
    MFC编程入门之二十六(常用控件:滚动条控件ScrollBar)
    MFC编程入门之二十五(常用控件:组合框控件ComboBox)
    MFC编程入门之二十四(常用控件:列表框控件ListBox)
    MFC编程入门之二十三(常用控件:按钮控件的编程实例)
    MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
  • 原文地址:https://www.cnblogs.com/LvLoveYuForever/p/5465137.html
Copyright © 2020-2023  润新知