package test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Date;
public class Yunxin {
/**
* 发送POST方法的请求 这是通过网易云通信生成验证码的方法
*
* @return 所代表远程资源的响应结果
*/
public static String sendMsg(String phone)
{
String appKey = "6dc2a2a44692c8371cf6cc55bdd614f8";
String appSecret = "ff5b983cd319";
String nonce = "herunze"; // 随机数(最大长度128个字符)
String curTime = String.valueOf((new Date()).getTime() / 1000L); // 当前UTC时间戳
//System.out.println("curTime: " + curTime);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);
//System.out.println("checkSum: " + checkSum);
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try
{
String url = "https://api.netease.im/sms/sendcode.action"; //网址是网易云通信提供的发送验证码的接口
//设置参数信息 mobile:手机号 codeLen:验证码位数 templateid:模板ID
String params = "mobile="+phone+"&codeLen=4&templateid=3063511";
System.out.println("parms:" + params);
//创建URL对象
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("AppKey", appKey);
conn.setRequestProperty("CheckSum", checkSum);
conn.setRequestProperty("CurTime", curTime);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
conn.setRequestProperty("Nonce", nonce);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(params);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
//System.out.println(conn);
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
//System.out.println("in"+in);
String line;
while ((line = in.readLine()) != null)
{
result += line;
}
} catch (Exception e)
{
System.out.println("发送 POST 请求出现异常!\n" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
} catch (IOException ex)
{
ex.printStackTrace();
}
}
// StringBuffer buf = new StringBuffer();
// return buf.charAt(buf.length()-6)+""+buf.charAt(buf.length()-5)+""+buf.charAt(buf.length()-4)+""+buf.charAt(buf.length()-3);
return result;
}
/**
*
* 网易云通信发送通知类信息
*
*/
//这里的参数根据自己需要传递的自己定
public static String sendMsg(String phone1,String phone2,String name,String ordernum,String ordertype,String systemtime)
{
String appKey = "6dc2a2a44692c8371cf6cc55bdd614f8";
String appSecret = "ff5b983cd319";
String nonce = "herunze"; // 随机数(最大长度128个字符)
String curTime = String.valueOf((new Date()).getTime() / 1000L); // 当前UTC时间戳
System.out.println("curTime: " + curTime);
String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);
System.out.println("checkSum: " + checkSum);
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try
{
//网址是网易云通信给的发送通知类短信所提供的的接口
String url = "https://api.netease.im/sms/sendtemplate.action";
// url编码;防止不识别中文
String ordernum1 = URLEncoder.encode(ordernum, "utf-8");
String name1 = URLEncoder.encode(name, "utf-8");
String systemtime1 = URLEncoder.encode(systemtime, "utf-8");
String ordertype1 = URLEncoder.encode(ordertype, "utf-8");
String params = "templateid=3050545&mobiles=[\""+phone1+"\",\""+phone2+"\"]"
+ "¶ms=" + "[\"" + ordernum1 + "\",\""+ name1 + "\",\""+ systemtime1 + "\",\""+ ordertype1 + "\"]";
System.out.println("params:" + params);
//创建URL对象
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("AppKey", appKey);
conn.setRequestProperty("CheckSum", checkSum);
conn.setRequestProperty("CurTime", curTime);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
conn.setRequestProperty("Nonce", nonce);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(params);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
System.out.println(conn);
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
System.out.println("in"+in);
String line;
while ((line = in.readLine()) != null)
{
result += line;
}
} catch (Exception e)
{
System.out.println("发送 POST 请求出现异常!\n" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally
{
try
{
if (out != null)
{
out.close();
}
if (in != null)
{
in.close();
}
} catch (IOException ex)
{
ex.printStackTrace();
}
}
return result;
}
}
//网易云通信开发文档:所有接口均面向开发者服务器端调用,用于计算CheckSum的AppSecret开发者应妥善保管,可在应用的服务器端存储和使用,但不应存储或传递到客户端,也不应在网页等前端代码中嵌入。
//属于网易云通信的一种校验方式
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' };
}