feat(ic): 实现了与其他系统进行HTTP POST交互的功能

- 新增了HttpPostOtherSysImpl类,实现了IHttpPostOtherSys接口
- 实现了调用LE系统接口的功能,包括获取token和发送POST请求
- 代码中使用了Fastjson、StringUtils等工具库- 异常处理使用了ExceptionUtils进行业务异常包装
This commit is contained in:
张明 2025-05-15 11:00:51 +08:00
parent 8cfa66336a
commit 28c3c1e74a
2 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,89 @@
package nc.bs.ic;
import com.alibaba.fastjson.JSONObject;
import nc.IHttpPostOtherSys;
import nc.hr.utils.PubEnv;
import nc.itf.scmpub.reference.uap.para.SysParaInitQuery;
import nc.pub.fa.common.util.StringUtils;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
@Override
public String callLE(String url, JSONObject json) {
String leip = SysParaInitQuery.getParaString(PubEnv.getPk_group(), "LEIP");
// String leip = "http://60.214.115.166:20003";
String baseurl = leip + url;
String token = this.getLEToken(leip);
Map<String, String> headers = new HashMap<>();
headers.put("x-token", token);
String postResult = doPost(baseurl, headers, json);
JSONObject result = JSONObject.parseObject(postResult);
String code = result.getString("code");
if (StringUtils.equals(code, "0")) {
return postResult;
} else {
String message = result.getString("message");
ExceptionUtils.wrappBusinessException(message);
return message;
}
}
private String getLEToken(String leip) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "nc_le");
jsonObject.put("password", "7qTx8aPxVQ3n3j3HaKxZvLepyCyNm6gk967V7qcmJZkfBbJFEQZqPdQJMgfQNWdQ");
String result = doPost(leip + "/login", null, jsonObject);
JSONObject parsed = JSONObject.parseObject(result);
String token = parsed.getString("token");
return token;
}
private String doPost(String baseurl, Map<String, String> map, JSONObject json) {
BufferedReader reader = null;
try {
URL url = new URL(baseurl);// 创建连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestMethod("POST"); // 设置请求方式
// 设置接收数据的格式
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
if (map != null) {
for (String key : map.keySet()) {
connection.setRequestProperty(key, map.get(key));
}
}
connection.connect();
// 一定要用BufferedReader 来接收响应 使用字节来接收响应的方法是接收不到内容的
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // UTF-8编码
if (json != null) {
out.append(json.toString());
}
out.flush();
out.close();
// 读取响应
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
String res = "";
while ((line = reader.readLine()) != null) {
res += line;
}
reader.close();
return res;
} catch (Exception e) {
// e.printStackTrace();
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,15 @@
package nc;
import com.alibaba.fastjson.JSONObject;
public interface IHttpPostOtherSys {
/**
* xuwjl@yonyou.com 2025年4月24日 15:24:14
* @param url LE的接口地址不包含ip
* @param json 入参
*/
public String callLE(String url, JSONObject json);
}