package nccloud.pubift.commen.impl.utils; import com.alibaba.fastjson.JSONObject; import nc.bs.logging.Log; import nc.vo.pub.BusinessException; import nccloud.pubift.commen.itf.utils.IHttpPostOtherSys; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.util.HashMap; import java.util.Map; 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"; private static final String LOG_INFO_NAME = "OALOG"; private static final Log obmlog = Log.getInstance(LOG_INFO_NAME); @Override public String callMes(String url, JSONObject json) { String mesip = "http://192.168.29.32"; String baseurl = mesip + url; String tokenValue = this.getMESToken(mesip); Map headers = new HashMap<>(); if (tokenValue != null) { headers.put("Cookie", ".ASPXAUTH=" + tokenValue); } return doPost(baseurl, headers, json); } /** * 发送数据到外部系统 */ public void sendToExternalSystem(String apiPath, Map requestData) throws BusinessException { try { obmlog.debug("HttpPostOtherSys request :" + JSONObject.toJSONString(requestData)); JSONObject jsonRequest = new JSONObject(requestData); String response = callMes(apiPath, jsonRequest); JSONObject jsonResponse = JSONObject.parseObject(response); obmlog.debug("三方接口返回::" + jsonResponse.toJSONString()); String success = jsonResponse.getString("Success"); if ("false".equals(success)) { String errorMessage = jsonResponse.getString("ErrorMessage"); if (errorMessage == null) { errorMessage = "No error message provided by the external system."; } throw new BusinessException("同步mes系统失败,错误消息:" + errorMessage); } } catch (BusinessException e) { throw e; } catch (Exception e) { throw new BusinessException("调用外部接口失败:" + e.getMessage(), e); } } /** * 业务请求post方法 */ private String doPost(String baseurl, Map headers, JSONObject jsonPayload) { try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(baseurl); // Set standard headers httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); // Set custom headers from the map if (headers != null) { for (Map.Entry entry : headers.entrySet()) { httpPost.setHeader(entry.getKey(), entry.getValue()); } } if (jsonPayload != null) { StringEntity stringEntity = new StringEntity(jsonPayload.toJSONString(), "UTF-8"); httpPost.setEntity(stringEntity); } try (CloseableHttpResponse response = httpClient.execute(httpPost)) { HttpEntity entity = response.getEntity(); String responseString = EntityUtils.toString(entity, "UTF-8"); return responseString; } } catch (IOException e) { // In a real application, use a proper logging framework // e.printStackTrace(); throw new RuntimeException("HTTP POST request to " + baseurl + " failed: " + e.getMessage(), e); } } // 提取 .ASPXAUTH 的值 - This method remains unchanged private String extractAspxAuth(String setCookieHeaderLine) { // setCookieHeaderLine 是一行完整的 Set-Cookie 响应头,例如: // ".ASPXAUTH=TOKEN_VALUE; path=/; HttpOnly" // 或 ".ASPXAUTH=TOKEN_VALUE;" String[] cookieAttributes = setCookieHeaderLine.split(";"); // 按分号分割 for (String attribute : cookieAttributes) { String trimmedAttribute = attribute.trim(); // 去除前后空格 if (trimmedAttribute.startsWith(".ASPXAUTH=")) { return trimmedAttribute.substring(".ASPXAUTH=".length()); // 提取等号后的值 } } return null; // 没有找到 .ASPXAUTH } private String getMESToken(String mesIpBase) { JSONObject jsonObject = new JSONObject(); jsonObject.put("userId", USER_ID); jsonObject.put("password", PASSWORD); jsonObject.put("clientType", CLIENT_TYPE); jsonObject.put("epId", EP_ID); String loginUrl = mesIpBase + LOGIN_URL; try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpPost httpPost = new HttpPost(loginUrl); httpPost.setHeader("Content-Type", "application/json;charset=UTF-8"); StringEntity stringEntity = new StringEntity(jsonObject.toJSONString(), "UTF-8"); httpPost.setEntity(stringEntity); try (CloseableHttpResponse response = httpClient.execute(httpPost)) { Header[] setCookieHeaders = response.getHeaders("Set-Cookie"); if (setCookieHeaders != null) { for (Header header : setCookieHeaders) { String aspxAuthValue = extractAspxAuth(header.getValue()); if (aspxAuthValue != null) { return aspxAuthValue; // Found and return .ASPXAUTH token value } } } return null; // Return null if .ASPXAUTH cookie is not found, as per } } catch (IOException e) { throw new RuntimeException("Failed to get MESToken (cookie) from " + loginUrl + " due to IO issue: " + e.getMessage(), e); } } }