1、网易云信官网网址:https://netease.im/sms,注册登录后选择免费试用;
2、创建一个应用;
3、创建应用完成后,在未开通功能下找到短信服务,并申请试用或者开通;
4、选择第3步中的“短信模板管理”,然后选择"验证码模板"(记住你的模板id);
5、选择左侧菜单栏中“测试1”,回到应用主页面,点击App Key管理;
6、记住你的App Key和App Secret;
7、开始测试
CheckSumBuilder.java
1 package test; 2 3 import java.security.MessageDigest; 4 5 public class CheckSumBuilder { 6 7 // 计算并获取CheckSum 8 public static String getCheckSum(String appSecret, String nonce, String curTime) { 9 return encode("sha1", appSecret + nonce + curTime); 10 } 11 12 // 计算并获取md5值 13 public static String getMD5(String requestBody) { 14 return encode("md5", requestBody); 15 } 16 17 private static String encode(String algorithm, String value) { 18 if (value == null) { 19 return null; 20 } 21 try { 22 MessageDigest messageDigest 23 = MessageDigest.getInstance(algorithm); 24 messageDigest.update(value.getBytes()); 25 return getFormattedText(messageDigest.digest()); 26 } catch (Exception e) { 27 throw new RuntimeException(e); 28 } 29 } 30 private static String getFormattedText(byte[] bytes) { 31 int len = bytes.length; 32 StringBuilder buf = new StringBuilder(len * 2); 33 for (int j = 0; j < len; j++) { 34 buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); 35 buf.append(HEX_DIGITS[bytes[j] & 0x0f]); 36 } 37 return buf.toString(); 38 } 39 private static final char[] HEX_DIGITS = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, 40 ‘6‘, ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ }; 41 42 }
MobileMessageSend.java
1 package test; 2 3 import com.alibaba.fastjson.JSON; 4 import org.apache.http.HttpResponse; 5 import org.apache.http.NameValuePair; 6 import org.apache.http.client.entity.UrlEncodedFormEntity; 7 import org.apache.http.client.methods.HttpPost; 8 import org.apache.http.impl.client.CloseableHttpClient; 9 import org.apache.http.impl.client.HttpClients; 10 import org.apache.http.message.BasicNameValuePair; 11 import org.apache.http.util.EntityUtils; 12 13 import java.io.IOException; 14 import java.util.ArrayList; 15 import java.util.Date; 16 import java.util.List; 17 18 public class MobileMessageSend { 19 20 //发送验证码的请求路径URL 21 private static final String SERVER_URL="https://api.netease.im/sms/sendcode.action"; 22 //网易云信分配的账号,请替换你在管理后台应用下申请的Appkey 23 private static final String APP_KEY="你的AppKey"; 24 //网易云信分配的密钥,请替换你在管理后台应用下申请的appSecret 25 private static final String APP_SECRET="你的AppSecret"; 26 //随机数 27 private static final String NONCE="123456"; 28 //短信模板ID 29 private static final String TEMPLATEID="你的模板id"; 30 //手机号 31 //private static final String MOBILE="11111111"; 32 //验证码长度,范围4~10,默认为4 33 private static final String CODELEN="6"; 34 35 public static void sendMsg(String phone) throws IOException { 36 CloseableHttpClient httpclient = HttpClients.createDefault(); 37 HttpPost post = new HttpPost(SERVER_URL); 38 39 // 参考计算CheckSum的java代码,在上述文档的参数列表中,有CheckSum的计算文档示例 40 String curTime=String.valueOf((new Date().getTime()/1000L)); 41 String checkSum= CheckSumBuilder.getCheckSum(APP_SECRET,NONCE,curTime); 42 43 //设置请求的header 44 post.addHeader("AppKey",APP_KEY); 45 post.addHeader("Nonce",NONCE); 46 post.addHeader("CurTime",curTime); 47 post.addHeader("CheckSum",checkSum); 48 post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8"); 49 50 //设置请求参数 51 List<NameValuePair> nameValuePairs =new ArrayList<NameValuePair>(); 52 nameValuePairs.add(new BasicNameValuePair("mobile",phone)); 53 nameValuePairs.add(new BasicNameValuePair("templateid", TEMPLATEID)); 54 nameValuePairs.add(new BasicNameValuePair("codeLen", CODELEN)); 55 56 post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8")); 57 58 //执行请求 59 HttpResponse response=httpclient.execute(post); 60 String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8"); 61 62 //判断是否发送成功,发送成功返回true 63 String code= JSON.parseObject(responseEntity).getString("code"); 64 if (code.equals("200")){ 65 // String sms= JSON.parseObject(responseEntity).getString("obj"); 66 System.out.println("发送成功!"); 67 return; 68 } 69 System.out.println("发送失败!"); 70 } 71 }
SendMsg.java
1 package test; 2 3 import java.io.IOException; 4 5 public class SendMsg { 6 public static void main(String[] args) throws IOException { 7 String mobileNumber = "1111111111";//接收验证码的手机号码 8 MobileMessageSend.sendMsg(mobileNumber); 9 10 } 11 }
可参考网易云信官方api:https://dev.yunxin.163.com/docs/product/短信/短信接入示例
jar包和代码 百度网盘链接:https://pan.baidu.com/s/1HJQ9LyXEm-khZ4y69rfiGQ 提取码:tv0d
原文:https://www.cnblogs.com/wldbky/p/12172240.html