refactor(ic): 重构 HttpPostOtherSysImpl 类
- 修改了请求 MES 系统的方式 - 添加了获取 MES token 的方法- 优化了代码结构,增加了常量定义 - 新增了处理 cookie 的方法
This commit is contained in:
parent
e890d08065
commit
0763417ca8
|
@ -8,45 +8,46 @@ import nc.pub.fa.common.util.StringUtils;
|
||||||
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
|
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.net.HttpURLConnection;
|
import java.net.HttpURLConnection;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
|
public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
|
||||||
|
|
||||||
|
private static final String USER_ID = "BIP";
|
||||||
|
private static final String PASSWORD = "BIP@2025bip";
|
||||||
|
private static final String CLIENT_TYPE = "S";
|
||||||
|
private static final String EP_ID = "";
|
||||||
|
private static final String LOGIN_URL = "/GTHINKING/AjaxService/N_MISPRO/100208057.ashx/Login";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String callLE(String url, JSONObject json) {
|
public String callLE(String url, JSONObject json) {
|
||||||
String leip = SysParaInitQuery.getParaString(PubEnv.getPk_group(), "LEIP");
|
// String leip = SysParaInitQuery.getParaString(PubEnv.getPk_group(), "LEIP");
|
||||||
// String leip = "http://60.214.115.166:20003";
|
String mesip = "http://192.168.29.32";
|
||||||
String baseurl = leip + url;
|
String baseurl = mesip + url;
|
||||||
String token = this.getLEToken(leip);
|
String cookie = this.getMESToken(mesip);
|
||||||
Map<String, String> headers = new HashMap<>();
|
Map<String, String> headers = new HashMap<>();
|
||||||
headers.put("x-token", token);
|
headers.put("Set-Cookie", cookie);
|
||||||
String postResult = doPost(baseurl, headers, json);
|
return 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) {
|
private String getMESToken(String leip) {
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
jsonObject.put("username", "nc_le");
|
jsonObject.put("userId", USER_ID);
|
||||||
jsonObject.put("password", "7qTx8aPxVQ3n3j3HaKxZvLepyCyNm6gk967V7qcmJZkfBbJFEQZqPdQJMgfQNWdQ");
|
jsonObject.put("password", PASSWORD);
|
||||||
String result = doPost(leip + "/login", null, jsonObject);
|
jsonObject.put("clientType", CLIENT_TYPE);
|
||||||
JSONObject parsed = JSONObject.parseObject(result);
|
jsonObject.put("epId", EP_ID);
|
||||||
String token = parsed.getString("token");
|
return postCookie(leip + LOGIN_URL, null, jsonObject);
|
||||||
return token;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 业务请求post方法
|
||||||
|
*/
|
||||||
private String doPost(String baseurl, Map<String, String> map, JSONObject json) {
|
private String doPost(String baseurl, Map<String, String> map, JSONObject json) {
|
||||||
BufferedReader reader = null;
|
BufferedReader reader = null;
|
||||||
try {
|
try {
|
||||||
|
@ -86,4 +87,75 @@ public class HttpPostOtherSysImpl implements IHttpPostOtherSys {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取cookie的请求post
|
||||||
|
*/
|
||||||
|
private String postCookie(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();
|
||||||
|
|
||||||
|
// 写入请求体
|
||||||
|
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
|
||||||
|
if (json != null) {
|
||||||
|
out.append(json.toString());
|
||||||
|
}
|
||||||
|
out.flush();
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
// 读取响应头中的 Set-Cookie
|
||||||
|
Map<String, List<String>> headers = connection.getHeaderFields();
|
||||||
|
String setCookieHeader = null;
|
||||||
|
|
||||||
|
for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
|
||||||
|
if ("Set-Cookie".equalsIgnoreCase(entry.getKey())) {
|
||||||
|
setCookieHeader = entry.getValue().get(0); // 取第一个 Set-Cookie 头
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (setCookieHeader != null) {
|
||||||
|
return extractAspxAuth(setCookieHeader); // 返回 ASPXAUTH 的值
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} finally {
|
||||||
|
if (reader != null) {
|
||||||
|
try {
|
||||||
|
reader.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取 .ASPXAUTH 的值
|
||||||
|
private String extractAspxAuth(String cookieHeader) {
|
||||||
|
String[] cookies = cookieHeader.split("; ");
|
||||||
|
for (String cookie : cookies) {
|
||||||
|
if (cookie.startsWith(".ASPXAUTH=")) {
|
||||||
|
return cookie.substring(".ASPXAUTH=".length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue