使用网易云通信发送验证码,通知类型短信

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+"\"]" 
                        + "&params=" + "[\"" + 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' };
}
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值