You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					177 lines
				
				5.8 KiB
			
		
		
			
		
	
	
					177 lines
				
				5.8 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								package com.ynxbd.common.helper.common;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import lombok.extern.slf4j.Slf4j;
							 | 
						||
| 
								 | 
							
								import org.slf4j.Logger;
							 | 
						||
| 
								 | 
							
								import org.slf4j.LoggerFactory;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import java.io.BufferedReader;
							 | 
						||
| 
								 | 
							
								import java.io.IOException;
							 | 
						||
| 
								 | 
							
								import java.io.InputStreamReader;
							 | 
						||
| 
								 | 
							
								import java.io.OutputStreamWriter;
							 | 
						||
| 
								 | 
							
								import java.net.HttpURLConnection;
							 | 
						||
| 
								 | 
							
								import java.net.URL;
							 | 
						||
| 
								 | 
							
								import java.nio.charset.StandardCharsets;
							 | 
						||
| 
								 | 
							
								import java.util.Map;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * @Author wsq
							 | 
						||
| 
								 | 
							
								 * @Date 2021/3/19 9:50
							 | 
						||
| 
								 | 
							
								 * @Copyright @ 2020 云南新八达科技有限公司 All rights reserved.
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								@Slf4j
							 | 
						||
| 
								 | 
							
								public class SoapHelper {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 特殊字符解码
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param value 数据
							 | 
						||
| 
								 | 
							
								     * @return data
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String decodeValue(String value) {
							 | 
						||
| 
								 | 
							
								        return value.replaceAll("&", "&")
							 | 
						||
| 
								 | 
							
								                .replaceAll("<", "<")
							 | 
						||
| 
								 | 
							
								                .replaceAll(">", ">")
							 | 
						||
| 
								 | 
							
								                .replaceAll("'", "'")
							 | 
						||
| 
								 | 
							
								                .replaceAll(""", "\"");
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public static String getRespXmlData(String xml) {
							 | 
						||
| 
								 | 
							
								        if (xml == null) {
							 | 
						||
| 
								 | 
							
								            return null;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        xml = decodeValue(xml);
							 | 
						||
| 
								 | 
							
								        String leftSign = "<Response>";
							 | 
						||
| 
								 | 
							
								        String rightSign = "</Response>";
							 | 
						||
| 
								 | 
							
								        int leftIndex = xml.indexOf(leftSign);
							 | 
						||
| 
								 | 
							
								        int rightIndex = xml.lastIndexOf(rightSign);
							 | 
						||
| 
								 | 
							
								        return xml.substring(leftIndex, rightIndex + rightSign.length());
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 创建一个连接
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param projectName    请求项目名
							 | 
						||
| 
								 | 
							
								     * @param soapUrl        soapURL
							 | 
						||
| 
								 | 
							
								     * @param soapAction     soapAction
							 | 
						||
| 
								 | 
							
								     * @param soapRequestXml 请求XML参数
							 | 
						||
| 
								 | 
							
								     * @return 数据
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String post(String projectName, String soapUrl, String soapAction, String soapRequestXml) {
							 | 
						||
| 
								 | 
							
								        return createConnection(projectName, soapUrl, soapAction, "POST", soapRequestXml);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 创建一个连接
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param projectName    请求项目名
							 | 
						||
| 
								 | 
							
								     * @param soapUrl        soapURL
							 | 
						||
| 
								 | 
							
								     * @param soapAction     soapAction
							 | 
						||
| 
								 | 
							
								     * @param soapRequestXml 请求XML参数
							 | 
						||
| 
								 | 
							
								     * @return 数据
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String get(String projectName, String soapUrl, String soapAction, String soapRequestXml) {
							 | 
						||
| 
								 | 
							
								        return createConnection(projectName, soapUrl, soapAction, "GET", soapRequestXml);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 创建一个连接
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param projectName    请求项目名
							 | 
						||
| 
								 | 
							
								     * @param soapUrl        soapURL
							 | 
						||
| 
								 | 
							
								     * @param soapAction     soapAction
							 | 
						||
| 
								 | 
							
								     * @param soapRequestXml 请求XML参数
							 | 
						||
| 
								 | 
							
								     * @return 数据
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String createConnection(String projectName, String soapUrl, String soapAction, String httpType, String soapRequestXml) {
							 | 
						||
| 
								 | 
							
								        if (soapUrl == null) {
							 | 
						||
| 
								 | 
							
								            return null;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        String respData = null; // 返回数据
							 | 
						||
| 
								 | 
							
								        HttpURLConnection connection = null;
							 | 
						||
| 
								 | 
							
								        OutputStreamWriter out = null;
							 | 
						||
| 
								 | 
							
								        BufferedReader reader = null;
							 | 
						||
| 
								 | 
							
								        try {
							 | 
						||
| 
								 | 
							
								            // 第一步:创建服务地址
							 | 
						||
| 
								 | 
							
								            URL url = new URL(soapUrl);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            // 第二步:打开一个通向服务地址的连接
							 | 
						||
| 
								 | 
							
								            connection = (HttpURLConnection) url.openConnection();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            // 第三步:设置参数
							 | 
						||
| 
								 | 
							
								            // 3.1发送方式设置:POST必须大写
							 | 
						||
| 
								 | 
							
								            connection.setRequestMethod(httpType);
							 | 
						||
| 
								 | 
							
								            // 3.2设置数据格式:content-msgInterface
							 | 
						||
| 
								 | 
							
								            connection.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
							 | 
						||
| 
								 | 
							
								            if (soapAction != null && !"".equals(soapAction)) {
							 | 
						||
| 
								 | 
							
								                connection.setRequestProperty("SOAPAction", soapAction);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            // 3.3设置输入输出,因为默认新创建的connection没有读写权限,
							 | 
						||
| 
								 | 
							
								            connection.setDoInput(true);
							 | 
						||
| 
								 | 
							
								            connection.setDoOutput(true);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            // 将信息以流的方式发送出去
							 | 
						||
| 
								 | 
							
								            out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
							 | 
						||
| 
								 | 
							
								            // 第四步:组织SOAP数据,发送请求
							 | 
						||
| 
								 | 
							
								            out.append(soapRequestXml);
							 | 
						||
| 
								 | 
							
								            out.flush();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            // 第五步:接收服务端响应
							 | 
						||
| 
								 | 
							
								            int responseCode = connection.getResponseCode();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            StringBuilder sb = new StringBuilder();
							 | 
						||
| 
								 | 
							
								            String temp;
							 | 
						||
| 
								 | 
							
								            reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
							 | 
						||
| 
								 | 
							
								            while ((temp = reader.readLine()) != null) {
							 | 
						||
| 
								 | 
							
								                sb.append(temp);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            if (responseCode == 200) { // 请求成功
							 | 
						||
| 
								 | 
							
								                respData = sb.toString();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            } else {
							 | 
						||
| 
								 | 
							
								                log.error("{} 请求失败: {}", projectName, sb);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        } catch (IOException e) {
							 | 
						||
| 
								 | 
							
								            log.error("{} 请求异常: {}", projectName, e.getMessage());
							 | 
						||
| 
								 | 
							
								            ErrorHelper.println(e);
							 | 
						||
| 
								 | 
							
								        } finally {
							 | 
						||
| 
								 | 
							
								            try {
							 | 
						||
| 
								 | 
							
								                if (out != null) out.close();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								                if (reader != null) reader.close();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								                if (connection != null) connection.disconnect();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            } catch (IOException e) {
							 | 
						||
| 
								 | 
							
								                e.printStackTrace();
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return respData;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 组合请求数据
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param params 数据
							 | 
						||
| 
								 | 
							
								     * @return 组合好的请求xml数据
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static String requestParams(String sys, Map<String, Object> params) {
							 | 
						||
| 
								 | 
							
								        StringBuilder paramsXml = new StringBuilder();
							 | 
						||
| 
								 | 
							
								        String key;
							 | 
						||
| 
								 | 
							
								        Object value;
							 | 
						||
| 
								 | 
							
								        for (Map.Entry<String, Object> entry : params.entrySet()) {
							 | 
						||
| 
								 | 
							
								            key = entry.getKey();
							 | 
						||
| 
								 | 
							
								            value = entry.getValue();
							 | 
						||
| 
								 | 
							
								            value = value == null ? "" : value.toString();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            paramsXml.append("<").append(key).append(">").append(value).append("</").append(key).append(">");
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        log.info("{} <Request>" + paramsXml + "</Request>", sys);
							 | 
						||
| 
								 | 
							
								        return paramsXml.toString();
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |