|
@@ -0,0 +1,644 @@
|
|
|
+package nckd.nc2kd.base.common.utils;
|
|
|
+
|
|
|
+import kd.bos.dataentity.entity.DynamicObject;
|
|
|
+import kd.bos.logging.Log;
|
|
|
+import kd.bos.logging.LogFactory;
|
|
|
+import kd.bos.logorm.LogORM;
|
|
|
+import kd.bos.servicehelper.BusinessDataServiceHelper;
|
|
|
+import org.apache.http.Header;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpGet;
|
|
|
+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.HttpClientBuilder;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import org.json.JSONObject;
|
|
|
+import org.json.XML;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.w3c.dom.Document;
|
|
|
+import org.xml.sax.InputSource;
|
|
|
+import javax.xml.XMLConstants;
|
|
|
+import javax.xml.parsers.DocumentBuilder;
|
|
|
+import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
+import javax.xml.transform.Transformer;
|
|
|
+import javax.xml.transform.TransformerConfigurationException;
|
|
|
+import javax.xml.transform.TransformerException;
|
|
|
+import javax.xml.transform.TransformerFactory;
|
|
|
+import javax.xml.transform.dom.DOMSource;
|
|
|
+import javax.xml.transform.stream.StreamResult;
|
|
|
+import java.io.*;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.URL;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * http请求工具类
|
|
|
+ */
|
|
|
+public class HttpRequestUtils {
|
|
|
+ public static final String DISALLOW_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl";
|
|
|
+ public static final String LOAD_EXTERNAL_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
|
|
|
+ public static final String EXTERNAL_GENERAL_ENTITIES = "http://xml.org/sax/features/external-general-entities";
|
|
|
+ public static final String EXTERNAL_PARAMETER_ENTITIES = "http://xml.org/sax/features/external-parameter-entities";
|
|
|
+ private static CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
+
|
|
|
+ public static final Log log = LogFactory.getLog(HttpRequestUtils.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Get请求
|
|
|
+ *
|
|
|
+ * @param url 请求地址
|
|
|
+ * @param headers 请求头
|
|
|
+ * @return 请求结果
|
|
|
+ */
|
|
|
+ public static String get(String url, Header[] headers) {
|
|
|
+ HttpGet httpGet = new HttpGet(url);
|
|
|
+ // 请求头中设置授权TOKEN
|
|
|
+ httpGet.setHeaders(headers);
|
|
|
+ String result = "";
|
|
|
+
|
|
|
+ try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
|
|
|
+ // 返回状态码
|
|
|
+ int statusCode = httpResponse.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode == 200) {
|
|
|
+ HttpEntity httpEntity = httpResponse.getEntity();
|
|
|
+ // 获取结果
|
|
|
+ result = EntityUtils.toString(httpEntity);
|
|
|
+ EntityUtils.consumeQuietly(httpEntity);
|
|
|
+
|
|
|
+ } else if (statusCode == 401) {
|
|
|
+ HttpEntity httpEntity = httpResponse.getEntity();
|
|
|
+ // 获取结果
|
|
|
+ result = EntityUtils.toString(httpEntity);
|
|
|
+ EntityUtils.consumeQuietly(httpEntity);
|
|
|
+ }
|
|
|
+ } catch (IOException ignored) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * POST请求
|
|
|
+ *
|
|
|
+ * @param url 请求地址
|
|
|
+ * @param headers 请求头
|
|
|
+ * @param data 请求参数
|
|
|
+ * @return 请求结果
|
|
|
+ */
|
|
|
+ public static String post(String url, Header[] headers, String data) {
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setHeaders(headers);
|
|
|
+ // 设置参数
|
|
|
+ StringEntity stringEntity = new StringEntity(data, StandardCharsets.UTF_8);
|
|
|
+ stringEntity.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
|
|
+ httpPost.setEntity(stringEntity);
|
|
|
+ String result = "";
|
|
|
+ try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
|
|
|
+ // 返回状态码
|
|
|
+ int statusCode = httpResponse.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode == 200) {
|
|
|
+ HttpEntity httpEntity = httpResponse.getEntity();
|
|
|
+ // 获取结果
|
|
|
+ result = EntityUtils.toString(httpEntity);
|
|
|
+ EntityUtils.consumeQuietly(httpEntity);
|
|
|
+ } else {
|
|
|
+ InetAddress localHost = InetAddress.getLocalHost();
|
|
|
+ //writeLog("http请求", "http请求异常" + statusCode, "url:" + url + "IP:" + localHost, data, EntityUtils.toString(httpResponse.getEntity()));
|
|
|
+ }
|
|
|
+ } catch (IOException ignored) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * POST请求(可设置请求时长)
|
|
|
+ *
|
|
|
+ * @param url 请求地址
|
|
|
+ * @param headers 请求头
|
|
|
+ * @param data 请求参数
|
|
|
+ * @param timeout 单位:毫秒
|
|
|
+ * @return 请求结果
|
|
|
+ */
|
|
|
+ public static String post2(String url, Header[] headers, String data, int timeout) {
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setHeaders(headers);
|
|
|
+ // 设置请求时长
|
|
|
+ RequestConfig.Builder configBuilder = RequestConfig.custom();
|
|
|
+ configBuilder.setConnectTimeout(timeout);
|
|
|
+ configBuilder.setSocketTimeout(timeout);
|
|
|
+ RequestConfig requestConfig = configBuilder.build();
|
|
|
+ httpPost.setConfig(requestConfig);
|
|
|
+
|
|
|
+ // 设置参数
|
|
|
+ StringEntity stringEntity = new StringEntity(data, StandardCharsets.UTF_8);
|
|
|
+ stringEntity.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
|
|
+ httpPost.setEntity(stringEntity);
|
|
|
+ String result = "";
|
|
|
+ try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
|
|
|
+ // 返回状态码
|
|
|
+ int statusCode = httpResponse.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode == 200) {
|
|
|
+ HttpEntity httpEntity = httpResponse.getEntity();
|
|
|
+ // 获取结果
|
|
|
+ result = EntityUtils.toString(httpEntity);
|
|
|
+ EntityUtils.consumeQuietly(httpEntity);
|
|
|
+ }
|
|
|
+ } catch (IOException ignored) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post请求发送xml(application/soap+xml)
|
|
|
+ *
|
|
|
+ * @param serviceUrl 服务地址
|
|
|
+ * @param data 数据
|
|
|
+ * @param timeout 超时时间
|
|
|
+ * @return 请求结果
|
|
|
+ */
|
|
|
+ public static String postXml(String serviceUrl, String data, int timeout) {
|
|
|
+ try {
|
|
|
+ // HttpURLConnection 发送SOAP请求
|
|
|
+ URL url = new URL(serviceUrl);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setUseCaches(false);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setConnectTimeout(timeout);
|
|
|
+ conn.setReadTimeout(timeout);
|
|
|
+
|
|
|
+ DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
|
|
|
+ dos.write(data.getBytes(StandardCharsets.UTF_8));
|
|
|
+ dos.flush();
|
|
|
+
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String line;
|
|
|
+ StringBuilder strBuf = new StringBuilder();
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ strBuf.append(line);
|
|
|
+ }
|
|
|
+ dos.close();
|
|
|
+ reader.close();
|
|
|
+
|
|
|
+
|
|
|
+ // 创建一个DocumentBuilder对象
|
|
|
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
+ factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
|
|
+
|
|
|
+ //factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); //禁用DTDs (doctypes),几乎可以防御所有xml实体攻击
|
|
|
+
|
|
|
+ // 禁用DTDs
|
|
|
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
|
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
|
+
|
|
|
+ // 禁用DTDs的另一种方式(如果上述特性不受支持)
|
|
|
+ factory.setExpandEntityReferences(false);
|
|
|
+ DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
+
|
|
|
+ // 使用DocumentBuilder解析XML字符串
|
|
|
+ Document document = builder.parse(new InputSource(new StringReader(strBuf.toString())));
|
|
|
+ // 将XML转换为JSONObject
|
|
|
+ JSONObject jsonObject = XML.toJSONObject(getStringFromDocument(document));
|
|
|
+
|
|
|
+ // 输出JSON
|
|
|
+ return jsonObject.toString(2); // 可以指定缩进空格数,这里是2
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post请求发送xml(text/xml)
|
|
|
+ *
|
|
|
+ * @param serviceUrl 服务地址
|
|
|
+ * @param data 数据
|
|
|
+ * @param timeout 超时时间
|
|
|
+ * @return 请求结果
|
|
|
+ */
|
|
|
+ public static String postXml2(String serviceUrl, String data, int timeout) {
|
|
|
+ try {
|
|
|
+ // HttpURLConnection 发送SOAP请求
|
|
|
+ URL url = new URL(serviceUrl);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setUseCaches(false);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setConnectTimeout(timeout);
|
|
|
+ conn.setReadTimeout(timeout);
|
|
|
+
|
|
|
+ DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
|
|
|
+ dos.write(data.getBytes(StandardCharsets.UTF_8));
|
|
|
+ dos.flush();
|
|
|
+
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String line = null;
|
|
|
+ StringBuilder strBuf = new StringBuilder();
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ strBuf.append(line);
|
|
|
+ }
|
|
|
+ dos.close();
|
|
|
+ reader.close();
|
|
|
+
|
|
|
+
|
|
|
+ // 创建一个DocumentBuilder对象
|
|
|
+ //创建DocumentBuilderFactory实例
|
|
|
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
+ factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
|
|
+ // 禁用DTDs
|
|
|
+// factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
|
|
+// factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
|
|
+
|
|
|
+ // 禁用DTDs的另一种方式(如果上述特性不受支持)
|
|
|
+ factory.setExpandEntityReferences(false);
|
|
|
+ DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
+
|
|
|
+ // 使用DocumentBuilder解析XML字符串
|
|
|
+ Document document = builder.parse(new InputSource(new StringReader(strBuf.toString())));
|
|
|
+ // 将XML转换为JSONObject
|
|
|
+ JSONObject jsonObject = XML.toJSONObject(getStringFromDocument(document));
|
|
|
+
|
|
|
+ // 输出JSON
|
|
|
+ return jsonObject.toString(2); // 可以指定缩进空格数,这里是2
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post请求发送xml,可以指定SOAPAction(text/xml)
|
|
|
+ *
|
|
|
+ * @param serviceUrl 服务地址
|
|
|
+ * @param data 数据
|
|
|
+ * @param timeout 超时时间
|
|
|
+ * @param soapAction SOAPAction
|
|
|
+ * @return 请求结果
|
|
|
+ */
|
|
|
+ public static String postXml3(String serviceUrl, String data, int timeout, String soapAction) {
|
|
|
+ try {
|
|
|
+ // HttpURLConnection 发送SOAP请求
|
|
|
+ URL url = new URL(serviceUrl);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
|
|
|
+ conn.setRequestProperty("SOAPAction", soapAction);
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setUseCaches(false);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setConnectTimeout(timeout);
|
|
|
+ conn.setReadTimeout(timeout);
|
|
|
+
|
|
|
+ DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
|
|
|
+ dos.write(data.getBytes(StandardCharsets.UTF_8));
|
|
|
+ dos.flush();
|
|
|
+
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String line ;
|
|
|
+ StringBuilder strBuf = new StringBuilder();
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ strBuf.append(line);
|
|
|
+ }
|
|
|
+ dos.close();
|
|
|
+ reader.close();
|
|
|
+
|
|
|
+
|
|
|
+ // 创建一个DocumentBuilder对象
|
|
|
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
+ factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
|
|
+ //如果不能禁用DTDs,可以使用下两项,必须两项同时存在
|
|
|
+
|
|
|
+ factory.setFeature("http://xml.org/sax/features/external-general-entities", false); //防止外部普通实体POC 攻击
|
|
|
+ factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); //防止外部参数实体POC攻击
|
|
|
+
|
|
|
+
|
|
|
+ // 禁用DTDs的另一种方式(如果上述特性不受支持)
|
|
|
+ factory.setExpandEntityReferences(false);
|
|
|
+ DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
+
|
|
|
+ // 使用DocumentBuilder解析XML字符串
|
|
|
+ Document document = builder.parse(new InputSource(new StringReader(strBuf.toString())));
|
|
|
+ // 将XML转换为JSONObject
|
|
|
+ JSONObject jsonObject = XML.toJSONObject(getStringFromDocument(document));
|
|
|
+
|
|
|
+ // 输出JSON
|
|
|
+ return jsonObject.toString(2); // 可以指定缩进空格数,这里是2
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将Document对象转换为字符串
|
|
|
+ * @param document Document对象
|
|
|
+ * @return 字符串
|
|
|
+ */
|
|
|
+ private static String getStringFromDocument(Document document) {
|
|
|
+ try {
|
|
|
+ // 使用Transformer将Document转换为String
|
|
|
+ TransformerFactory tf = TransformerFactory.newInstance();
|
|
|
+ tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
|
|
|
+ tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
|
|
|
+ Transformer transformer = tf.newTransformer();
|
|
|
+ StringWriter writer = new StringWriter();
|
|
|
+ transformer.transform(new DOMSource(document), new StreamResult(writer));
|
|
|
+ return writer.toString();
|
|
|
+ } catch (TransformerConfigurationException e) {
|
|
|
+ log.error("处理配置错误", e);
|
|
|
+ // 处理配置错误
|
|
|
+ return null;
|
|
|
+ } catch (TransformerException e) {
|
|
|
+ log.error("处理转换错误", e);
|
|
|
+ // 处理转换错误
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("处理其他类型的异常", e);
|
|
|
+ // 处理其他类型的异常
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建动态SOAP请求
|
|
|
+ *
|
|
|
+ * @param soapParams 参数
|
|
|
+ * @param elementName 方法名称
|
|
|
+ * @param namespaceURI 命名空间
|
|
|
+ * @param soapAction SOAP动作
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String buildDynamicSoapRequest(com.alibaba.fastjson.JSONObject soapParams, String elementName, String namespaceURI, String soapAction) {
|
|
|
+ if (soapAction == null) {
|
|
|
+ soapAction = "http://www.w3.org/2003/05/soap-envelope";
|
|
|
+ }
|
|
|
+ StringBuilder xmlBuilder = new StringBuilder();
|
|
|
+ xmlBuilder.append("<soapenv:Envelope xmlns:soapenv=\"").append(soapAction).append("\" ")
|
|
|
+ .append("xmlns:emp=\"").append(namespaceURI).append("\">")
|
|
|
+ .append("<soapenv:Header/><soapenv:Body>")
|
|
|
+ .append("<").append(elementName).append(">");
|
|
|
+
|
|
|
+ // 将动态参数添加到SOAP请求
|
|
|
+ for (String key : soapParams.keySet()) {
|
|
|
+ xmlBuilder.append("<emp:").append(key).append(">")
|
|
|
+ .append(soapParams.get(key))
|
|
|
+ .append("</emp:").append(key).append(">");
|
|
|
+ }
|
|
|
+
|
|
|
+ xmlBuilder.append("</").append(elementName).append("></soapenv:Body></soapenv:Envelope>");
|
|
|
+
|
|
|
+ return xmlBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取access_token请求参数处理
|
|
|
+ public static String buildOASoapRequest(com.alibaba.fastjson.JSONObject soapParams, String elementName, String namespaceURI, String soapAction) {
|
|
|
+ if (soapAction == null) {
|
|
|
+ soapAction = "http://www.w3.org/2003/05/soap-envelope";
|
|
|
+ }
|
|
|
+ StringBuilder xmlBuilder = new StringBuilder();
|
|
|
+ xmlBuilder.append("<soapenv:Envelope xmlns:soapenv=\"").append(soapAction).append("\" ")
|
|
|
+ .append("xmlns:out=\"").append(namespaceURI).append("\">")
|
|
|
+ .append("<soapenv:Header/><soapenv:Body>")
|
|
|
+ .append("<").append(elementName).append(">");
|
|
|
+
|
|
|
+ for (String key : soapParams.keySet()) {
|
|
|
+ xmlBuilder.append("<out:").append(key).append(">")
|
|
|
+ .append(soapParams.get(key))
|
|
|
+ .append("</out:").append(key).append(">");
|
|
|
+ }
|
|
|
+ xmlBuilder.append("</").append(elementName).append("></soapenv:Body></soapenv:Envelope>");
|
|
|
+ return xmlBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * oa系统校验人员soap拼接
|
|
|
+ *
|
|
|
+ * @param soapParams
|
|
|
+ * @param elementName
|
|
|
+ * @param namespaceURI
|
|
|
+ * @param soapAction
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String buildDynamicSoapRequest2(com.alibaba.fastjson.JSONObject soapParams, String elementName, String namespaceURI, String soapAction) {
|
|
|
+ if (soapAction == null) {
|
|
|
+ soapAction = "http://www.w3.org/2003/05/soap-envelope";
|
|
|
+ }
|
|
|
+ StringBuilder xmlBuilder = new StringBuilder();
|
|
|
+ xmlBuilder.append("<soapenv:Envelope xmlns:soapenv=\"").append(soapAction).append("\" ")
|
|
|
+ .append("xmlns:aut=\"").append(namespaceURI).append("\">")
|
|
|
+ .append("<soapenv:Header/><soapenv:Body>")
|
|
|
+ .append("<").append(elementName).append(">");
|
|
|
+
|
|
|
+ // 将动态参数添加到SOAP请求
|
|
|
+ for (String key : soapParams.keySet()) {
|
|
|
+ xmlBuilder.append("<aut:").append(key).append(">")
|
|
|
+ .append(soapParams.get(key))
|
|
|
+ .append("</aut:").append(key).append(">");
|
|
|
+ }
|
|
|
+
|
|
|
+ xmlBuilder.append("</").append(elementName).append("></soapenv:Body></soapenv:Envelope>");
|
|
|
+
|
|
|
+ return xmlBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * ERP交叉校验soap拼接
|
|
|
+ *
|
|
|
+ * @param headerParams header参数
|
|
|
+ * @param soapParams body参数
|
|
|
+ * @param functionName 函数名
|
|
|
+ * @param elementName 方法名
|
|
|
+ * @param username erp认证用户名
|
|
|
+ * @param password erp认证用户密码
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String buildDynamicSoapRequest3(com.alibaba.fastjson.JSONObject headerParams, com.alibaba.fastjson.JSONObject soapParams, String functionName, String elementName, String username, String password) {
|
|
|
+ StringBuilder xmlBuilder = new StringBuilder();
|
|
|
+ xmlBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ")
|
|
|
+ .append("xmlns:sfy=\"http://xmlns.oracle.com/apps/cux/soaprovider/plsql/sfy_ap_statement_payment/\" ")
|
|
|
+ .append("xmlns:val=\"http://xmlns.oracle.com/apps/cux/soaprovider/plsql/sfy_ap_statement_payment/").append(functionName).append("/\">")
|
|
|
+ .append("<soapenv:Header><sfy:SOAHeader>");
|
|
|
+
|
|
|
+
|
|
|
+ // 将动态参数添加到SOAP请求(请求头)
|
|
|
+ for (String key : headerParams.keySet()) {
|
|
|
+ xmlBuilder.append("<sfy:").append(key).append(">")
|
|
|
+ .append(headerParams.get(key))
|
|
|
+ .append("</sfy:").append(key).append(">");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加安全参数
|
|
|
+ xmlBuilder.append("</sfy:SOAHeader>")
|
|
|
+ .append("<wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" mustUnderstand=\"1\">")
|
|
|
+ .append("<wsse:UsernameToken>")
|
|
|
+ .append("<wsse:Username>").append(username).append("</wsse:Username>")
|
|
|
+ .append("<wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">").append(password).append("</wsse:Password>")
|
|
|
+ .append("</wsse:UsernameToken></wsse:Security>");
|
|
|
+
|
|
|
+ xmlBuilder.append("</soapenv:Header><soapenv:Body>")
|
|
|
+ .append("<").append(elementName).append(">");
|
|
|
+ String[] split = elementName.split(":");
|
|
|
+ // 将动态参数添加到SOAP请求(参数)
|
|
|
+ for (String key : soapParams.keySet()) {
|
|
|
+ xmlBuilder.append("<").append(split[0]).append(":").append(key).append(">")
|
|
|
+ .append(soapParams.get(key))
|
|
|
+ .append("</").append(split[0]).append(":").append(key).append(">");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ xmlBuilder.append("</").append(elementName).append("></soapenv:Body></soapenv:Envelope>");
|
|
|
+
|
|
|
+ return xmlBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取企微打卡记录
|
|
|
+ *
|
|
|
+ * @param empid 工号
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @param checkindtype 打卡类型,固定2
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String buildDynamicSoapRequest4(String empid, String startTime, String endTime, String checkindtype) {
|
|
|
+ StringBuilder xmlBuilder = new StringBuilder();
|
|
|
+ xmlBuilder.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:wor=\"http://www.primeton.com/workWechatQueryUtilService\" >")
|
|
|
+ .append("<soapenv:Header />");
|
|
|
+
|
|
|
+ xmlBuilder.append("<soapenv:Body>")
|
|
|
+ .append("<wor:queryWXattendanceData>")
|
|
|
+ .append("<wor:userList>").append(empid).append("</wor:userList>")
|
|
|
+ .append("<wor:startTime>").append(startTime).append("</wor:startTime>")
|
|
|
+ .append("<wor:endTime>").append(endTime).append("</wor:endTime>")
|
|
|
+ .append("<wor:checkindtype>").append(checkindtype).append("</wor:checkindtype>")
|
|
|
+ .append("</wor:queryWXattendanceData>");
|
|
|
+
|
|
|
+
|
|
|
+ xmlBuilder.append("</soapenv:Body></soapenv:Envelope>");
|
|
|
+
|
|
|
+ return xmlBuilder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String buildOAPurchaseXml(HashMap<String, String> mapHeader, LinkedHashMap<String, String> mapParam, List<LinkedHashMap<String, String>> listEntryParam) {
|
|
|
+ StringBuffer xmlBuilder = new StringBuffer();
|
|
|
+ String sp = "\n";
|
|
|
+ xmlBuilder.append("<soapenv:Envelope\n" +
|
|
|
+ " xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
|
|
|
+ " xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"\n" +
|
|
|
+ " xmlns:imp=\"http://xmlns.oracle.com/apps/cux/soaprovider/plsql/sfy_po_oa_interface_pkg/import_pr_interface/\"\n" +
|
|
|
+ " xmlns:sfy=\"http://xmlns.oracle.com/apps/cux/soaprovider/plsql/sfy_po_oa_interface_pkg/\">\n")
|
|
|
+ //添加hearder信息
|
|
|
+ .append(" <soapenv:Header>\n" +
|
|
|
+ " <sfy:SOAHeader>\n");
|
|
|
+ for (String key : mapHeader.keySet()) {
|
|
|
+ xmlBuilder.append("<").append(key).append(">").append(mapHeader.get(key)).append("</").append(key).append(">").append(sp);
|
|
|
+ }
|
|
|
+ xmlBuilder.append(
|
|
|
+ " </sfy:SOAHeader>\n" +
|
|
|
+ " <wsse:Security mustUnderstand=\"1\">\n" +
|
|
|
+ " <wsse:UsernameToken>\n" +
|
|
|
+ " <wsse:Username>oa2erp</wsse:Username>\n" +
|
|
|
+ " <wsse:Password Type=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText\">1q2w#E$R</wsse:Password>\n" +
|
|
|
+ " </wsse:UsernameToken>\n" +
|
|
|
+ " </wsse:Security>\n" +
|
|
|
+ " </soapenv:Header>\n")
|
|
|
+ .append(" <soapenv:Body>\n" +
|
|
|
+ " <imp:InputParameters>\n");
|
|
|
+ for (String key : mapParam.keySet()) {
|
|
|
+ xmlBuilder.append("<imp:").append(key + ">").append(mapParam.get(key)).append("</imp:").append(key).append(">").append(sp);
|
|
|
+ }
|
|
|
+ xmlBuilder.append("<imp:P_PR_TBL_TYPE>");
|
|
|
+ for (int i = 0; i < listEntryParam.size(); i++) {
|
|
|
+ xmlBuilder.append(sp).append("<imp:P_PR_TBL_TYPE_ITEM>").append(sp);
|
|
|
+ LinkedHashMap<String, String> mapEntryParam = listEntryParam.get(i);
|
|
|
+ for (String key : mapEntryParam.keySet()) {
|
|
|
+ xmlBuilder.append("<imp:").append(key).append(">").append(mapEntryParam.get(key)).append("</imp:").append(key).append(">").append(sp);
|
|
|
+ }
|
|
|
+ xmlBuilder.append("</imp:P_PR_TBL_TYPE_ITEM>").append(sp);
|
|
|
+ }
|
|
|
+ xmlBuilder.append("</imp:P_PR_TBL_TYPE>");
|
|
|
+
|
|
|
+ xmlBuilder.append(
|
|
|
+ "\n</imp:InputParameters>\n" +
|
|
|
+ " </soapenv:Body>\n" +
|
|
|
+ "</soapenv:Envelope>\n");
|
|
|
+ return xmlBuilder.toString();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String postHtml(String url, Header[] headers, String data, int timeout) {
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setHeaders(headers);
|
|
|
+ // 设置请求时长
|
|
|
+ RequestConfig.Builder configBuilder = RequestConfig.custom();
|
|
|
+ configBuilder.setConnectTimeout(timeout);
|
|
|
+ configBuilder.setSocketTimeout(timeout);
|
|
|
+ RequestConfig requestConfig = configBuilder.build();
|
|
|
+ httpPost.setConfig(requestConfig);
|
|
|
+
|
|
|
+ // 设置参数
|
|
|
+ StringEntity stringEntity = new StringEntity(data, StandardCharsets.UTF_8);
|
|
|
+ stringEntity.setContentType("application/x-www-form-urlencoded");
|
|
|
+ httpPost.setEntity(stringEntity);
|
|
|
+ String result = "";
|
|
|
+ try (CloseableHttpResponse httpResponse = httpClient.execute(httpPost)) {
|
|
|
+ // 返回状态码
|
|
|
+ int statusCode = httpResponse.getStatusLine().getStatusCode();
|
|
|
+ if (statusCode == 200) {
|
|
|
+ HttpEntity httpEntity = httpResponse.getEntity();
|
|
|
+ // 获取结果
|
|
|
+ result = EntityUtils.toString(httpEntity);
|
|
|
+ EntityUtils.consumeQuietly(httpEntity);
|
|
|
+ }
|
|
|
+ } catch (IOException ignored) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* *//**
|
|
|
+ * 写入日志
|
|
|
+ *
|
|
|
+ * @param name 操作人
|
|
|
+ * @param opName 操作名称
|
|
|
+ * @param opDesc 操作描述
|
|
|
+ * @param parameter 操作参数
|
|
|
+ * @param message 错误信息
|
|
|
+ *//*
|
|
|
+ public static void writeLog(String name, String opName, String opDesc, String parameter, String message) {
|
|
|
+ DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject(SendOaErrorLogEnum.DATA_OBJECT_TYPE_NAME);
|
|
|
+ dynamicObject.set(SendOaErrorLogEnum.USERNAME.getKey(), name);
|
|
|
+ dynamicObject.set(SendOaErrorLogEnum.OPNAME.getKey(), opName);
|
|
|
+ dynamicObject.set(SendOaErrorLogEnum.OPDESC.getKey(), opDesc);
|
|
|
+ dynamicObject.set(SendOaErrorLogEnum.OPDATE.getKey(), new Date());
|
|
|
+ dynamicObject.set(SendOaErrorLogEnum.PARAMETER2_TAG.getKey(), parameter);
|
|
|
+ dynamicObject.set(SendOaErrorLogEnum.ERRORMSG2_TAG.getKey(), message);
|
|
|
+
|
|
|
+ List<DynamicObject> list = new ArrayList<>();
|
|
|
+ list.add(dynamicObject);
|
|
|
+ LogORM.create().insert(list);
|
|
|
+ }*/
|
|
|
+}
|