From 08beb58febbf1dd2841fdc1e6d38c722f9a10db2 Mon Sep 17 00:00:00 2001 From: fang <123456> Date: Wed, 13 Nov 2024 09:47:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=85=E5=8A=9E=5F1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/shkd/sys/sys/midservice/test01.java | 135 ++++++++++++++++++ .../sys/midservice/utils/RequestClient.java | 34 +++-- 2 files changed, 159 insertions(+), 10 deletions(-) create mode 100644 sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/test01.java diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/test01.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/test01.java new file mode 100644 index 0000000..ef16d6d --- /dev/null +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/test01.java @@ -0,0 +1,135 @@ +//package shkd.sys.sys.midservice; +// +//import org.apache.commons.lang.StringUtils; +//import org.apache.commons.logging.Log; +//import org.apache.commons.logging.LogFactory; +//import org.apache.http.HttpEntity; +//import org.apache.http.NameValuePair; +//import org.apache.http.ParseException; +//import org.apache.http.client.entity.UrlEncodedFormEntity; +//import org.apache.http.client.methods.CloseableHttpResponse; +//import org.apache.http.client.methods.HttpPost; +//import org.apache.http.impl.client.CloseableHttpClient; +//import org.apache.http.message.BasicNameValuePair; +//import org.apache.http.util.EntityUtils; +// +//import java.io.IOException; +//import java.security.MessageDigest; +//import java.security.NoSuchAlgorithmException; +//import java.util.ArrayList; +//import java.util.Iterator; +//import java.util.List; +//import java.util.Map; +// +///** +// * 单据界面 +// */ +//public class test01 { +// +// private static final Log LOG = LogFactory.getLog(test01.class); +// private static final CloseableHttpClient httpClient = null; +// public static final String CHARSET = "UTF-8"; +// +// public static final String MD5(String s) { +// return MD5(s, ""); +// } +// public static String doPost(String url, Map header, Map params, String charset) throws IOException { +// if (StringUtils.isBlank(url)) { +// return null; +// } else { +// List pairs = null; +// if (params != null && !params.isEmpty()) { +// pairs = new ArrayList(params.size()); +// Iterator var5 = params.entrySet().iterator(); +// +// while(var5.hasNext()) { +// Map.Entry entry = (Map.Entry)var5.next(); +// String value = (String)entry.getValue(); +// if (value != null) { +// pairs.add(new BasicNameValuePair((String)entry.getKey(), value)); +// } +// } +// } +// +// HttpPost httpPost = new HttpPost(url); +// if (null != header && header.size() > 0) { +// Iterator var17 = header.entrySet().iterator(); +// +// while(var17.hasNext()) { +// Map.Entry headerSet = (Map.Entry)var17.next(); +// httpPost.setHeader((String)headerSet.getKey(), (String)headerSet.getValue()); +// } +// } +// +// if (pairs != null && pairs.size() > 0) { +// httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8")); +// } +// +// CloseableHttpResponse response = null; +// +// try { +// response = httpClient.execute(httpPost); +// int statusCode = response.getStatusLine().getStatusCode(); +// if (statusCode != 200) { +// httpPost.abort(); +// throw new RuntimeException("HttpClient,error status code :" + statusCode); +// } +// +// HttpEntity entity = response.getEntity(); +// String result = null; +// if (entity != null) { +// result = EntityUtils.toString(entity, "utf-8"); +// } +// +// EntityUtils.consume(entity); +// String var10 = result; +// return var10; +// } catch (ParseException var14) { +// var14.printStackTrace(); +// } finally { +// if (response != null) { +// response.close(); +// } +// +// } +// +// return null; +// } +// } +// +// +// public static String MD5(String strSrc, String key) { +// try { +// MessageDigest md5 = MessageDigest.getInstance("MD5"); +// byte[] b = strSrc.getBytes("utf-8"); +// md5.update(b); +// String result = ""; +// byte[] temp = md5.digest(key.getBytes("utf-8")); +// String s = ""; +// byte[] var7 = temp; +// int var8 = temp.length; +// +// for(int var9 = 0; var9 < var8; ++var9) { +// byte bb = var7[var9]; +// s = s + bb + " "; +// } +// +// for(int i = 0; i < temp.length; ++i) { +// result = result + Integer.toHexString(255 & temp[i] | -256).substring(6); +// } +// +// return result; +// } catch (NoSuchAlgorithmException var11) { +// LOG.error("md5异常", var11); +// } catch (Exception var12) { +// LOG.error("md5异常", var12); +// } +// +// return ""; +// } +// +// +// public static void main(String[] args) throws Exception { +// System.out.println(MD5("scicsiku")); +// } +//} diff --git a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/utils/RequestClient.java b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/utils/RequestClient.java index 414c1a8..a062800 100644 --- a/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/utils/RequestClient.java +++ b/sys/shkd-sys-sys/src/main/java/shkd/sys/sys/midservice/utils/RequestClient.java @@ -7,11 +7,13 @@ import kd.bos.servicehelper.BusinessDataServiceHelper; import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.bos.util.HttpClientUtils; import kd.bos.util.StringUtils; -import kd.ebg.aqap.banks.srcb.dc.services.utils.SmUtil; +import kd.data.idi.util.HexUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; @@ -29,15 +31,15 @@ public class RequestClient { private String protocal;//http private String ip;// 域名 private String port;// 端口 - private String clientKey;// clientKey - private String clientSecret_scict;// clientSecret-scict + private String clientKey;// 系统标识 +// private String clientSecret_scict;// clientSecret-scict private RequestClient() { this.protocal = System.getProperty("backlog-protocal"); this.ip = System.getProperty("backlog-ip"); this.port = System.getProperty("backlog-port"); this.clientKey= System.getProperty("clientKey-scict"); - this.clientSecret_scict= System.getProperty("clientSecret-scict"); + // this.clientSecret_scict= System.getProperty("clientSecret-scict"); } /** * 获取中台服务客户端实例 @@ -57,12 +59,14 @@ public class RequestClient { * @param body JSON格式字符串 * @return 响应结果报文 */ - public String callService(String url, String body) { - Map header = new HashMap<>(); + public String callService(String url, String body) throws NoSuchAlgorithmException { + /*Map header = new HashMap<>(); header.put("systemid", this.clientKey); header.put("sign",this.clientSecret_scict); - header.put("Content-Type", "application/x-www-form-urlencoded"); + header.put("Content-Type", "application/x-www-form-urlencoded");*/ // header.put("Content-Type", "application/json; charset=utf-8"); + Map headers = getHeaders(); + logger.info("获得headers的方法结束"+JSON.toJSONString(headers)); StringBuffer urlStr = new StringBuffer(); urlStr.append(this.protocal).append("://").append(this.ip); if (!StringUtils.isEmpty(this.port)) { @@ -73,14 +77,24 @@ public class RequestClient { String responseText = null; try { //直接POST请求 - responseText = HttpClientUtils.postjson(urlStr.toString(), header, body); - logger.info(String.format("###调用接口成功,请求URL:%s,请求Header:%s,请求Body:%s,响应报文:%s", urlStr, JSON.toJSONString(header), body, responseText)); + responseText = HttpClientUtils.postjson(urlStr.toString(), headers, body); + logger.info(String.format("###调用接口成功,请求URL:%s,请求Header:%s,请求Body:%s,响应报文:%s", urlStr, JSON.toJSONString(headers), body, responseText)); } catch (IOException e) { - logger.error(String.format("###调用接口异常:%s,请求URL:%s,请求Header:%s,请求Body:%s", e.getMessage(), urlStr, JSON.toJSONString(header), body), e); + logger.error(String.format("###调用接口异常:%s,请求URL:%s,请求Header:%s,请求Body:%s", e.getMessage(), urlStr, JSON.toJSONString(headers), body), e); } return responseText; } + private Map getHeaders() throws NoSuchAlgorithmException { + MessageDigest md5 = MessageDigest.getInstance("MD5"); + byte[] md5Bytes = md5.digest(this.clientKey.getBytes()); + String md5Str = HexUtil.bytes2Hex(md5Bytes).toUpperCase(); + HashMap map = new HashMap(); + map.put("systemid", this.clientKey); + map.put("sign", md5Str); + map.put("Content-Type", "application/x-www-form-urlencoded"); + return map; + } /*** * 保存待办日志 * @param title 标题