使用java发送短信业务代码

import cn.hutool.core.util.StrUtil;
import com.dlfxzp.common.constant.Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;

public class HttpSend {
    private static Log log = LogFactory.getLog(HttpSend.class);

    /**
     * 环境参数
     */
    //private static String environment = ((Environment) SpringUtils.getBean("environment")).getProperty("spring.profiles.active");

    /**
     * 短信发送接口
     * 建议不超过300字 + 100个手机号码,或者不超过800字 + 30个手机号码
     *
     * @param phone    接收人号码:多个之间用“,”分割,例如:130000000,12000000000
     * @param content  短信类容
     * @param sendTime 定时发送时间。可以为空,为空就是立即发送。定时发送格式为年月日时分,如:200803201615。,
     * @return int 大于0,这是成功的条数,小于等于0发送失败
     */
    public static int send(String phone, String content, String sendTime) {
        // todo 暂时注释掉,生产需要放开
//        if (!("prod".equals(environment) || "qa".equals(environment))) {
//            to = "";
//        }
        if (StrUtil.isBlank(phone)) {
            return 0;
        }
        try {
            String _content = java.net.URLEncoder.encode(content, "UTF-8");
            String _param = "Account=" + Constants.sms_user(你自己的用户) + "&Password=" + Constants.sms_pwd(你的密码)
                    + "&Phone=" + phone + "&Content=" + _content
                    + "&SubCode=&SendTime=" + sendTime;
            log.info("短信发送参数为:" + _param);
            URL url = null;
            HttpURLConnection urlConn = null;
            url = new URL(Constants.sms_url(你自己使用平台的url));
            urlConn = (HttpURLConnection) url.openConnection();
            urlConn.setRequestMethod("POST");
            urlConn.setDoOutput(true);
            OutputStream out = urlConn.getOutputStream();
            out.write(_param.getBytes("GBK"));
            out.flush();
            out.close();
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    urlConn.getInputStream(), "UTF-8"));
            StringBuffer sb = new StringBuffer();
            int ch;
            while ((ch = rd.read()) > -1) {
                sb.append((char) ch);
            }
            log.info("短信发送返回结果为:" + sb.toString());
            Document document = DocumentHelper.parseText(sb.toString());
            Element root = document.getRootElement();
            int res = Integer.parseInt(root.elementText("response"));
            rd.close();
            return res;
        } catch (Exception ex) {
            log.error("短信发送异常:", ex);
            return 0;
        }
    }

    public static String MD5Encode(String sourceString) {
        String resultString = null;
        try {
            resultString = new String(sourceString);
            MessageDigest md = MessageDigest.getInstance("MD5");
            resultString = byte2hexString(md.digest(resultString.getBytes()));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return resultString;
    }

    public static final String byte2hexString(byte[] bytes) {
        StringBuffer bf = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            if ((bytes[i] & 0xff) < 0x10) {
                bf.append("0");
            }
            bf.append(Long.toString(bytes[i] & 0xff, 16));
        }
        return bf.toString();
    }

    public static void main(String[] args) {
        HttpSend.send("137********", "定量分析指数专屏短信发送", "");
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值