点集短信Demo

---------------------------------------------------Controller

@Resource
private ISmsSendService smsSendService;

@PostMapping(value = "/innSmsSend")
@ApiOperation(value = "发送短信")
public String innSmsSend(@RequestBody SmsSend smsSend) {
    return smsSendService.innSendSms(smsSend);
}

---------------------------------------------------Vo

@ApiModelProperty(value = "手机号, 逗号隔开 :135123456**,136123456**", required = true)
private String mobile;

@ApiModelProperty(value = "短信内容", required = true)
private String content;

---------------------------------------------------Service

/**
 * 发送短信
 * @author zhusongtao
 * @date 2021/07/16 14:58
 */
String innSendSms(SmsSend smsSend);

---------------------------------------------------ServiceImpl

//短信内容发送
private static final String CONTENT_URL =  "http://sapi.appsms.cn:8088/msgHttp/json/mt";

private static final String CONTENT_URL_SMS =  "http://139.129.128.71:8086/msgHttp/json/mt";

//短信发送地址(https)
private static final String TEMPLATE_URL = "https://sapi.appsms.cn/msgHttp/json/mt";
//余额查询接口
private static final String STATUS_URL = "http://139.129.128.71:8086/msgHttp/json/balance";

private static final Integer callReadTimeOut = 10;
private static final Integer callConnectTimeOut = 5;
private static final Integer callMaxTotal = 5000;
private static final Integer callMaxPerRoute = 500;
volatile static HttpClient client;

 /**
 * 发送短信
 * @param smsSend :短信字段
 * @author zhusongtao
 * @date 2021/07/16 14:58
 */
@Override
public  String innSendSms(SmsSend smsSend) {
    if(ObjectUtil.isEmpty(smsSend)){
        throw new BusinessException("参数为null");
    }
    //手机号码,逗号隔开 :135123456**,136123456**
    String mobile = smsSend.getMobile();
    if(ObjectUtil.isEmpty(mobile)){
        throw new BusinessException("电话号码不能为空");
    }
    String resultContent = "";
    //接口账号-待确认是否存数据库维护?
    String account = "********";
    //接口密码-待确认是否存数据库维护?
    String password = "********";
    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    //短信发送地址
    HttpPost httpPost = new HttpPost(CONTENT_URL);
    List<BasicNameValuePair> formparams = new ArrayList<BasicNameValuePair>();
    long timestamps = System.currentTimeMillis();
    formparams.add(new BasicNameValuePair("account", account));
    //数字签名:(接口密码、手机号、时间戳32位MD5加密生成)
    formparams.add(new BasicNameValuePair("password", getMD532Str(password + mobile + timestamps)));
    //必填 手机号码,逗号隔开 :135123456**,136123456**
    formparams.add(new BasicNameValuePair("mobile", mobile));
    //必填 发送内容
    formparams.add(new BasicNameValuePair("content", smsSend.getContent() == null ? " " : smsSend.getContent()));
    //时间戳,短信发送当前时间毫秒数,生成数字签名用
    formparams.add(new BasicNameValuePair("timestamps", timestamps+""));

    UrlEncodedFormEntity uefEntity;
    try {
        long start = System.currentTimeMillis();
        uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
        httpPost.setEntity(uefEntity);
        //接口账号
        System.out.println("1 "+ EntityUtils.toString(httpPost.getEntity()));
        //post+具体地址+HTTP
        System.out.println("2 "+"httpPost.-->"+httpPost.toString());
        CloseableHttpResponse httpResponse = httpclient.execute(httpPost);
        System.out.println("3 "+"httpResponse-->"+httpResponse.toString());
        HttpEntity entity = httpResponse.getEntity();
        System.out.println("4 "+httpResponse.getStatusLine().toString());
        //响应格式
        if (entity != null) {
            resultContent = EntityUtils.toString(entity, "UTF-8");
            System.out.println("5\n "+resultContent);
        }
        long end = System.currentTimeMillis();
        System.out.println("cost -->"+(end -start));

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        //关闭连接,释放资源
        try {
            if(response != null){
                response.close();
            }
            httpclient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return resultContent;
}

/**
 * @param sourceStr 接口密码、手机号、时间戳组合
 * @description MD5 32位加密
 * @author zhusongtao
 * @date 2021/07/16 15:18
 */
public  static String getMD532Str(String sourceStr) {
    String result = "";
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(sourceStr.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        //result = buf.toString().substring(8, 24);
        result = buf.toString();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值