Java网易短信接入demo
2017-03-23 10:21
183 查看
由于最近使用云信添加短信接入功能,记录一下一些点避免时间久了忘记。
发短信
验证
获取计算checksum
亲测有效。
ps:这里用的是默认的短信模板,如果自定义了短信模板,传入参数的时候多加一个模板id就行了。
发短信
package com.lincoln.Untils; 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; import java.util.Map; public class Yunxin { public static void main(String[] args) throws Exception { System.out.println(sendMsg("158********")); } private static final String SERVER_URL = "https://api.netease.im/sms/sendcode.action";//发送验证码的请求路径URL private static final String APP_KEY = "*******";//网易云信分配的账号 private static final String APP_SECRET = "*******";//网易云信分配的密钥 private static final String NONCE = "123456";//随机数 public static String sendMsg(String phone) 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<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("mobile", phone)); 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 "success"; } return "error"; } }
验证
package com.lincoln.Untils; /** * @author 李智 * @date 2017/3/22 */ import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; 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; /** * 校验验证码工具类 * * @author Administrator */ public class MobileMessageCheck { public static void main(String[] args) throws Exception { System.out.println(checkMsg("158********","1234")); } private static final String SERVER_URL = "https://api.netease.im/sms/verifycode.action";//校验验证码的请求路径URL private static final String APP_KEY = "*******";//网易云信分配的账号 private static final String APP_SECRET = "*******";//网易云信分配的密钥 private static final String NONCE = "123456";//随机数 public static String checkMsg(String phone, String sum) 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<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("mobile", phone)); nameValuePairs.add(new BasicNameValuePair("code", sum)); 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 "success"; } return "error"; } }
获取计算checksum
package com.lincoln.Untils; import java.security.MessageDigest; /** * @author 李智 * @date 2017/3/22 */ public class CheckSumBuilder { // 计算并获取CheckSum public static String getCheckSum(String appSecret, String nonce, String curTime) { return encode("sha1", appSecret + nonce + curTime); } // 计算并获取md5值 public static String getMD5(String requestBody) { return encode("md5", requestBody); } 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 buf = new StringBuilder(len * 2); for (int j = 0; j < len; j++) { buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]); buf.append(HEX_DIGITS[bytes[j] & 0x0f]); } return buf.toString(); } private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; }
亲测有效。
ps:这里用的是默认的短信模板,如果自定义了短信模板,传入参数的时候多加一个模板id就行了。
相关文章推荐
- Java网易短信接入demo
- [置顶] 用java实现发送手机短信的DEMO
- JAVA接入短信接口,实现短信验证码功能
- 易班开放平台第三方网站接入的Demo(Java_SDK)的步骤
- Idea-Java接入银联支付的Demo
- 阿里云通信短信服务java版(基于webservice的restful风格的demo)
- 开源Java短信投票系统Demo【仅作收藏】
- 网易短信java实现
- 微信公众平台后台接入Java版Demo
- java调用短信api接口发送短信demo实例
- Java三行代码搞定MD5加密,测试5c短信网关的demo
- java接入短信平台源码
- Idea-Java接入银联支付的Demo
- JAVA接入短信接口,实现短信验证码功能
- URL 授权访问另外一种方法,利用 Java 1.1 访问密码保护的 URL(9588http短信通道接入)
- java 实现阿里大于 发送短信demo
- 盈科短信猫的C#接入程序DEMO
- java接入秒嘀API实现发送短信功能
- java 发送短信demo
- java发送手机短信demo