之前找了好久没找到,最后还是同学帮忙的
@Value("${sendMessage.url}")
private String sendUrl;
@Value("${sendMessage.userId}")
private String userId;
@Value("${sendMessage.pwd}")
private String pwd;
@Value("${sendMessage.struid}")
private String struid;
@Value("${sendMessage.sendTemplete}")
private String sendTemplete;
public Boolean send(String userId,String pwd,String struid,String sendUrl,String sendPhone, String sendContent,String sendDate) throws IOException {
Boolean flag = false;
InputStream in = null;
HttpURLConnection conn = null;
try {
File file = ResourceUtils.getFile("classpath:static\\sendMessage.xml");
InputStream input = new FileInputStream(file);
Map<String, String> params = new HashMap<>();
params.put("Userid", userId);
params.put("Pwd", pwd);
params.put("struid", struid);
params.put("strRecNo", sendPhone);
params.put("strcontent", sendTemplete.replace("$num",sendContent));
params.put("strsendDate", sendDate);
String postData = readSoapFile(input, params);
StringBuilder response = new StringBuilder();
// 创建URL对象
URL url = new URL(sendUrl);
// 连接WebService
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setFollowRedirects(true);
conn.setAllowUserInteraction(false);
conn.setRequestMethod("POST");
conn.setConnectTimeout(120000);
conn.setReadTimeout(120000);
OutputStream out = conn.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
logger.error(params.toString());
writer.write(postData);
writer.close();
out.close();
if (conn.getResponseCode() != 200) {
in = conn.getErrorStream();
} else {
in = conn.getInputStream();
flag = true;
}
InputStreamReader iReader = new InputStreamReader(in,"UTF-8");
BufferedReader bReader = new BufferedReader(iReader);
// 处理返回结果
String line;
while ((line = bReader.readLine()) != null) {
response.append(line + "\n");
}
for(int i = 0;i<response.length();i++ ){
logger.info(response.toString());
}
iReader.close();
bReader.close();
in.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
conn.disconnect();
}
return flag;
}
public String readSoapFile(InputStream input, Map<String, String> params) throws Exception {
byte[] b = new byte[1024];
int len = 0;
int temp = 0;
while ((temp = input.read()) != -1) {
b[len] = (byte) temp;
len++;
}
String soapxml = new String(b);
return replace(soapxml, params);
}
public String replace(String param, Map<String, String> params) throws Exception {
//拼凑占位符使用正则表达式替换之
String result = param;
if (params != null && !params.isEmpty()) {
//拼凑占位符
for (Map.Entry<String, String> entry : params.entrySet()) {
String name = "\$" + entry.getKey();
Pattern p = Pattern.compile(name);
Matcher m = p.matcher(result);
if (m.find()) {
result = m.replaceAll(entry.getValue());
}
}
}
return result;
}
其中由于我这边内网外网差别 ,开始的那些接口网址,帐号,密码参数我是从yml配置文件里读的 后面调用send方法传电话号码 ,短信信息等内容进去 返回的内容还没判断是否成功可自行完善,我是打印出来response可以看到了。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="xxxx.xxxx" xmlns:xsd="xxxx.xxx" xmlns:soap="http://xxxxx.xxxx/">
<soap:Body>
<CheckAndSMS xmlns="xxxx.xxxx">
<Userid>$Userid</Userid>
<Pwd>$Pwd</Pwd>
<struid>$struid</struid>
<btype>1</btype>
<strRecNo>$strRecNo</strRecNo>
<strcontent>$strcontent</strcontent>
<strsendDate>$strsendDate</strsendDate>
</CheckAndSMS>
</soap:Body>
</soap:Envelope>
xml模版是从对应网站考下来的 访问对应接口的网址里面就有模版,特此记录
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/2220.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...