您的位置:首页 > 编程语言 > Java开发

Java网易短信接入demo

2017-03-23 10:21 183 查看
由于最近使用云信添加短信接入功能,记录一下一些点避免时间久了忘记。

发短信

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 短信 网易