Commit f6ccbb26 authored by qinxunjia's avatar qinxunjia

请求方式优化

parent 9eb6393b
package com.jz.sms.api;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.chuanglan.dto.CLBizResponse;
import com.jz.sms.chuanglan.dto.CLNotifyRequest;
import com.jz.sms.chuanglan.dto.ReportDto;
......@@ -8,10 +9,7 @@ import com.jz.sms.chuanglan.service.ChuangLanSmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -27,7 +25,7 @@ public class ChuangLanApi {
@PostMapping("/notify")
public String notify(@RequestParam CLNotifyRequest notifyInfo) {
log.info("创蓝异步通知接口入参:{}", notifyInfo);
log.info("创蓝异步通知接口入参:{}", JSONObject.toJSONString(notifyInfo));
chuangLanSmsService.asyncNotify(notifyInfo);
......@@ -37,12 +35,10 @@ public class ChuangLanApi {
return response;
}
@PostMapping("/report")
public String report(@RequestParam ReportDto report) {
log.info("回送报告接口入参:{}", report);
@GetMapping("/report")
public String report(ReportDto report) {
log.info("回送报告接口入参:{}", JSONObject.toJSONString(report));
CLBizResponse responses = chuangLanSmsService.report(report);
String response = "success";
log.info("创蓝异步通知接口出参:{}", response);
return response;
......
......@@ -41,7 +41,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
@Override
public CLBizResponse createTemplate(String content, String type) throws Exception {
int i = 1 / 0;
// TODO 测试,不往上游请求
log.info("进入创蓝创建短信接口模板的逻辑:{}", content);
Map<String, Object> requestMap = new HashMap<>();
String appId;
......@@ -51,13 +51,13 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
appId = "49";
} else if ("marketing".equals(type)) {
appId = "52";
content = content.replaceAll("回TD退订", "");
} else {
// TODO 自定义异常,发送警告
log.error("错误,传入的模板类型未知,类型为:{}", type);
return new CLBizResponse(ResponseCode.SYSTEM_ERROR, "未知的模板类型:" + type, 0);
}
content = content.replaceAll("回TD退订", "");
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
String encrypt = Md5Util.encrypt(new StringBuffer(username).append(password).append(timestamp).toString());
requestMap.put("username", username);
......@@ -135,7 +135,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
password = ChuangLanSMSConfig.marketingPassword;
log.info("发送短信的模式为:{营销类}");
} else {
log.info("发送短信的模式为:{未知类型}");
log.info("发送短信的模式为:{未知类型},type:{}", type);
throw new Exception("123");
}
content = "【九章数据】" + content;
......@@ -143,9 +143,9 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
CLSendVariableRequest request = new CLSendVariableRequest(account, password, content, params, "true", batchId);
String requestJson = JSONObject.toJSONString(request);
log.info("【{}】发送固定内容短信,请求上游数据为:{},\r\n请求地址为:{}", batchId, requestJson, ChuangLanSMSConfig.sendVariable);
log.info("【{}】发送变量内容短信,请求上游数据为:{},\r\n请求地址为:{}", batchId, requestJson, ChuangLanSMSConfig.sendVariable);
String responseStr = ChuangLanSmsUtil.sendSmsByPost(ChuangLanSMSConfig.sendVariable, requestJson);
log.info("【{}】发送固定内容短信,上游返回数据为:{}", batchId, responseStr);
log.info("【{}】发送变量内容短信,上游返回数据为:{}", batchId, responseStr);
if (StringUtils.isBlank(responseStr)) {
return new CLBizResponse(ResponseCode.UPSTREAM_BLANK);
......@@ -188,6 +188,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
@Override
public CLBizResponse report(ReportDto report) {
// TODO 状态报告要特殊处理,一个个接受,批量发送
String uid = report.getUid();
return null;
......
......@@ -9,19 +9,17 @@ import java.net.URL;
//import java.io.PrintWriter;
/**
*
* @author tianyh
* @Description:HTTP 请求
*/
public class ChuangLanSmsUtil {
/**
*
* @author tianyh
* @Description
* @param path
* @param postContent
* @return String
* @throws
* @author tianyh
* @Description
*/
public static String sendSmsByPost(String path, String postContent) {
URL url = null;
......@@ -35,14 +33,14 @@ public class ChuangLanSmsUtil {
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Charset", "UTF-8");
// httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// printWriter.write(postContent);
// printWriter.flush();
httpURLConnection.connect();
OutputStream os=httpURLConnection.getOutputStream();
OutputStream os = httpURLConnection.getOutputStream();
os.write(postContent.getBytes("UTF-8"));
os.flush();
......@@ -58,7 +56,48 @@ public class ChuangLanSmsUtil {
}
br.close();
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String sendTemplateByPost(String path, String postContent) {
URL url = null;
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) {
......
......@@ -204,9 +204,7 @@ public class MessageServiceImpl implements MessageService {
}
}
}
sendParams.append(";");
} else {
sendParams.append(",");
}
String type = templateInfo.getType();
......@@ -272,7 +270,9 @@ public class MessageServiceImpl implements MessageService {
// 封装参数
StringBuilder sendParams = new StringBuilder();
int i = 0;
for (JSONObject json : data) {
i++;
Set<String> keys = json.keySet();
String mobile = json.getString("_audienceId");
json.remove("name");
......@@ -295,12 +295,15 @@ public class MessageServiceImpl implements MessageService {
}
}
}
if (i != data.size()) {
sendParams.append(";");
}
} else {
if (i != data.size()) {
sendParams.append(",");
}
}
System.out.println(sendParams.toString());
}
DmBatchInfo dmInfo = new DmBatchInfo();
dmInfo.setDmBatchId(batchId);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment