package com.matrix.system.shopXcx.api.tools;
|
|
import com.alibaba.fastjson.JSON;
|
import com.matrix.core.tools.LogUtil;
|
import com.matrix.system.shopXcx.api.tools.model.request.SmsSendRequest;
|
import com.matrix.system.shopXcx.api.tools.model.response.SmsSendResponse;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
|
import java.io.BufferedReader;
|
import java.io.InputStreamReader;
|
import java.io.OutputStream;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
|
/**
|
* @author jyy
|
*/
|
@Service("sMSTools")
|
public class SMSTools {
|
/**
|
* 用户平台API账号(非登录账号,示例:N1234567)
|
*/
|
@Value("${sms.login_account}")
|
private String loginAccount;
|
/**
|
* 用户平台API密码(非登录密码)
|
*/
|
@Value("${sms.login_password}")
|
private String loginPassword;
|
/**
|
* 用户平台请求URL
|
*/
|
@Value("${sms.request_url}")
|
private String requestUrl;
|
|
/**
|
* @param phone 手机号码
|
* @param msg 短信内容
|
* @return
|
*/
|
public SmsSendResponse sendMsg(String phone, String msg) {
|
//请求地址请登录253云通讯自助通平台查看或者询问您的商务负责人获取
|
String smsSingleRequestServerUrl = requestUrl + "/msg/send/json";
|
msg = "【新诚智慧】" + msg;
|
//状态报告
|
String report = "true";
|
SmsSendRequest smsSingleRequest = new SmsSendRequest(loginAccount, loginPassword, msg, phone, report);
|
String requestJson = JSON.toJSONString(smsSingleRequest);
|
LogUtil.info("sendMsg requestParam: {}", requestJson);
|
String response = sendSmsByPost(smsSingleRequestServerUrl, requestJson);
|
SmsSendResponse smsSingleResponse = JSON.parseObject(response, SmsSendResponse.class);
|
LogUtil.info("sendMsg response: {}", smsSingleResponse.toString());
|
return smsSingleResponse;
|
}
|
|
public String sendSmsByPost(String path, String postContent) {
|
URL url;
|
try {
|
url = new URL(path);
|
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
|
// 提交模式
|
httpURLConnection.setRequestMethod("POST");
|
//连接超时 单位毫秒
|
httpURLConnection.setConnectTimeout(10000);
|
//读取超时 单位毫秒
|
httpURLConnection.setReadTimeout(10000);
|
|
// 发送POST请求必须设置如下两行
|
httpURLConnection.setDoOutput(true);
|
httpURLConnection.setDoInput(true);
|
httpURLConnection.setRequestProperty("Charset", "UTF-8");
|
httpURLConnection.setRequestProperty("Content-Type", "application/json");
|
// PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
|
// printWriter.write(postContent);
|
// printWriter.flush();
|
httpURLConnection.connect();
|
OutputStream os = httpURLConnection.getOutputStream();
|
os.write(postContent.getBytes("UTF-8"));
|
os.flush();
|
StringBuilder sb = new StringBuilder();
|
int httpRspCode = httpURLConnection.getResponseCode();
|
if (httpRspCode == HttpURLConnection.HTTP_OK) {
|
// 开始获取数据
|
BufferedReader br = new BufferedReader(
|
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
|
String line = null;
|
while ((line = br.readLine()) != null) {
|
sb.append(line);
|
}
|
br.close();
|
return sb.toString();
|
}
|
} catch (Exception e) {
|
LogUtil.error("发送请求失败", e);
|
return null;
|
}
|
return null;
|
}
|
|
}
|