网易云短信接口调入(java)

1 需要两个jar包  httpclient-4.3.6.jar和httpcore-4.3.3.jar jar包

2.验证码生成工具

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'};

}

3.(1)验证码

public class SendCode {

    //发送验证码的请求路径URL
    private static final String
            SERVER_URL="xxxxxxxxxx";
    //网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
    private static final String
            APP_KEY="xxxxxxxxxxxxxx";
    //网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret
    private static final String APP_SECRET="xxxxxxxxxxxx";
    //随机数
    private static final String NONCE="123456";
    //短信模板ID
    private static final String TEMPLATEID="xxxxxxxxx";
    //验证码长度,范围4~10,默认为4
    private static final String CODELEN="6";

    // MOBILE 手机号
    public static void sendCode(String MOBILE) throws Exception {

        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<NameValuePair> nvps = new ArrayList<NameValuePair>();
        /*
         * 1.如果是模板短信,请注意参数mobile是有s的,详细参数配置请参考“发送模板短信文档”
         * 2.参数格式是jsonArray的格式,例如 "['13888888888','13666666666']"
         * 3.params是根据你模板里面有几个参数,那里面的参数也是jsonArray格式
         */
        nvps.add(new BasicNameValuePair("templateid", TEMPLATEID));
        nvps.add(new BasicNameValuePair("mobile",  MOBILE.toString()));     //JSON.toJSONString(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状态表
         */
        System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
    }


}

3.(2)通知类或运用类 


public class SendNoticeCode {

    //发送验证码的请求路径URL
    private static final String
            SERVER_URL="xxxxxxxxxx";
    //网易云信分配的账号,请替换你在管理后台应用下申请的Appkey
    private static final String
            APP_KEY="xxxxxxxxxxxxxxxxxxxxxxxx";
    //网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret
    private static final String APP_SECRET="xxxxxxxxxxxxxxxxxx";
    //随机数
    private static final String NONCE="123456";
    //短信模板ID
    private static final String TEMPLATEID="xxxxxxxxxxxxxxxxx";

    //MOBILES       手机号,接收者号码列表,JSONArray格式,限制接收者号码个数最多为100个
    //PARAMS        短信参数列表,用于依次填充模板,JSONArray格式,每个变量长度不能超过30字,对于不包含变量的模板,不填此参数表示模板即短信全文内容
    public static void sendNoticeCode(String MOBILES, String PARAMS) throws Exception{

        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<NameValuePair> nvps = new ArrayList<NameValuePair>();
        /*
         * 1.如果是模板短信,请注意参数mobile是有s的,详细参数配置请参考“发送模板短信文档”
         * 2.参数格式是jsonArray的格式,例如 "['13888888888','13666666666']"
         * 3.params是根据你模板里面有几个参数,那里面的参数也是jsonArray格式
         */
        nvps.add(new BasicNameValuePair("templateid", TEMPLATEID));
        nvps.add(new BasicNameValuePair("mobiles", MOBILES));
        nvps.add(new BasicNameValuePair("params", PARAMS));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));

        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);
        /*
         * 1.打印执行结果,打印结果一般会200、315、403、404、413、414、500
         * 2.具体的code有问题的可以参考官网的Code状态表
         */
        System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));
}

    public static void main(String[] args) throws Exception {
        SendNoticeCode sen = new SendNoticeCode();
        sen.sendNoticeCode( "['13888888888','13666666666']", "['xxxx','xxxx']");
    }

}

附发送多人,编辑发送内容 



        //电话号码
		JSONArray json = new JSONArray();
		json.add("151****7851");
		
		//短信内容(此处的短信内容是你在网易短信模板申请中的变量【%s】这种,
		//值得注意的是你申请了多少个%S变量就必须填写多少个但是每个变量不能大于30个字:比如我申请 
         了11个那么我就要传11个变量)
		//如果参数少了会提示{"code":414,"msg":"miss param"}大概意思是说缺少参数
		JSONArray json1 = new JSONArray();

		//json1.add("222");
		//json1.add("https:www.dingteam.com");
		
		
		System.out.println(json1.toString());
		
		//json.add(value)
		List<NameValuePair> nvp = new ArrayList<NameValuePair>();
		nvp.add(new BasicNameValuePair("templateid",TEMPLATEID));
		
		nvp.add(new BasicNameValuePair("mobiles","[\"187709***\"]"));
		
		nvp.add(new BasicNameValuePair("params",json1.toString()));
		
	
		
		httpPost.setEntity(new UrlEncodedFormEntity(nvp,"utf-8"));
		
		HttpResponse response = httpCline.execute(httpPost);
		 
		System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));

	}

猜你喜欢

转载自blog.csdn.net/IT_LuoSha/article/details/88554643