zoukankan      html  css  js  c++  java
  • 网易云信(验证码短信接口接入)

    准备工具类: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;
        }
    }
  • 相关阅读:
    MySQL Stack Buffer Overflow Linux x86 32bits
    WordPress Browser Rejector 插件"wppath"远程文件包含漏洞
    JBoss Enterprise Application Platform安全绕过漏洞
    启动NDuiker项目
    java基础>Java常用类库 小强斋
    MyEclipse6.5安装SVN插件的三种方法 小强斋
    jsp>Jsp语法 小强斋
    java基础>Java常用类库 小强斋
    jsp>Jsp语法 小强斋
    java基础>正则表达式 小强斋
  • 原文地址:https://www.cnblogs.com/LvLoveYuForever/p/5465137.html
Copyright © 2011-2022 走看看