码迷,mamicode.com
首页 > 其他好文 > 详细

网易云信发送短信验证码

时间:2016-08-09 19:09:10      阅读:2034      评论:0      收藏:0      [点我收藏+]

标签:

  1 package com.hydsoft.hotnetworks.system.controller;
  2 
  3 import java.io.IOException;
  4 import java.io.UnsupportedEncodingException;
  5 import java.security.MessageDigest;
  6 import java.util.ArrayList;
  7 import java.util.Date;
  8 import java.util.List;
  9 
 10 import org.apache.http.HttpEntity;
 11 import org.apache.http.NameValuePair;
 12 import org.apache.http.client.ClientProtocolException;
 13 import org.apache.http.client.entity.UrlEncodedFormEntity;
 14 import org.apache.http.client.methods.CloseableHttpResponse;
 15 import org.apache.http.client.methods.HttpPost;
 16 import org.apache.http.impl.client.CloseableHttpClient;
 17 import org.apache.http.impl.client.HttpClients;
 18 import org.apache.http.message.BasicNameValuePair;
 19 import org.apache.http.util.EntityUtils;
 20 
 21 public class Test {
 22 
 23     public static void main(String args[]) {
 24         String url = "https://api.netease.im/sms/sendcode.action"; // 获取验证码的固定请求路径
 25         String appKey = "b9bfe886534643e110f8446d860ead7c";    //    应用的 key
 26         String appSecret = "610128b2fabe";    // 应用的 密码
 27         String nonce = "123456";    //    随机数
 28         String mobile = "18201623501";    //    目标手机号码
 29         String curTime=String.valueOf((new Date().getTime()/1000L));
 30         String checkSum = getCheckSum(appSecret, nonce, curTime);    //    得到 发送验证码必须的参数checkSum
 31         
 32         CloseableHttpClient httpclient = HttpClients.createDefault();  
 33         // 将参数保存到请求头中
 34         HttpPost httpPost = new HttpPost(url);
 35         httpPost.addHeader("AppKey",appKey);
 36         httpPost.addHeader("Nonce",nonce);
 37         httpPost.addHeader("CurTime",curTime);
 38         httpPost.addHeader("CheckSum",checkSum);
 39         httpPost.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
 40         
 41         
 42         List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
 43         formparams.add(new BasicNameValuePair("mobile", mobile));  
 44         UrlEncodedFormEntity uefEntity;  
 45         try {  
 46             uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");  
 47             httpPost.setEntity(uefEntity);  
 48             System.out.println("executing request " + httpPost.getURI());  
 49             CloseableHttpResponse response = httpclient.execute(httpPost);  
 50             try {  
 51                 HttpEntity entity = response.getEntity();  
 52                 if (entity != null) {   
 53                     System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));  
 54                 }  
 55             } finally {  
 56                 response.close();  
 57             }  
 58         } catch (ClientProtocolException e) {  
 59             e.printStackTrace();  
 60         } catch (UnsupportedEncodingException e1) {  
 61             e1.printStackTrace();  
 62         } catch (IOException e) {  
 63             e.printStackTrace();  
 64         } finally {  
 65             // 关闭连接,释放资源    
 66             try {  
 67                 httpclient.close(); 
 68             } catch (IOException e) {  
 69                 e.printStackTrace();  
 70             } 
 71         }
 72     }
 73     // 计算并获取CheckSum
 74     public static String getCheckSum(String appSecret, String nonce, String curTime) {
 75         return encode("sha1", appSecret + nonce + curTime);
 76     }
 77 
 78     // 计算并获取md5值
 79     public static String getMD5(String requestBody) {
 80         return encode("md5", requestBody);
 81     }
 82 
 83     private static String encode(String algorithm, String value) {
 84         if (value == null) {
 85             return null;
 86         }
 87         try {
 88             MessageDigest messageDigest
 89                     = MessageDigest.getInstance(algorithm);
 90             messageDigest.update(value.getBytes());
 91             return getFormattedText(messageDigest.digest());
 92         } catch (Exception e) {
 93             throw new RuntimeException(e);
 94         }
 95     }
 96     private static String getFormattedText(byte[] bytes) {
 97         int len = bytes.length;
 98         StringBuilder buf = new StringBuilder(len * 2);
 99         for (int j = 0; j < len; j++) {
100             buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
101             buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
102         }
103         return buf.toString();
104     }
105     private static final char[] HEX_DIGITS = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘,
106             ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ };
107 }

2016-08-09 18:28:51

网易云信发送短信验证码

标签:

原文地址:http://www.cnblogs.com/guofz/p/5754268.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!