当前位置 : 主页 > 编程语言 > c++ >

SOAP 调用封装类

来源:互联网 收集:自由互联 发布时间:2021-06-30
SoapCallUtils.java /** * * 描述: * * @package :com.andy.demo.soap * @author :wanglongjie */package com.andy.demo.soap;import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.io.OutputS
SoapCallUtils.java
/**
 * 

* 描述: *

* @package :com.andy.demo.soap
* @author :wanglongjie
*/ package com.andy.demo.soap; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** *

* 描述:调用Soap工具类 *

* * @author wanglongjie
* @version v1.0 2017年7月28日上午11:04:43 */ public class SoapCallUtils { private static Logger logger = LoggerFactory.getLogger(SoapCallUtils.class); /** * 缓冲区 */ private final static int SIZE = 1024 * 100; /** * 开始标识 */ private final static String FLAG_Begin = " "; /** * 结束标识 */ private final static String FLAG_End = " "; /** * *

* 描述:调用WebService方法 *

* * @Date 2017年7月28日上午11:20:23
* @param soapParam * SoapParam对象实例 * @return * @throws Exception */ public static final String call(SoapParam soapParam) throws Exception { InputStream is = null; try { is = getResponseForInputStream(soapParam); if (null == is) { throw new Exception("获取响应流失败!"); } ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[SIZE]; int len = 0; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); } buffer = os.toByteArray(); os.flush(); os.close(); is.close(); String result = os.toString("UTF-8"); logger.info("响应报文:{}", new String[] { result }); if (result.contains(FLAG_Begin) && result.contains(FLAG_End)) { return result.substring( result.indexOf(FLAG_Begin) + FLAG_Begin.length(), result.indexOf(FLAG_End)); } else { return result; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); return "调用失败:" + e.getMessage(); } } /** * *

* 描述:HTTP调用,获取请求的Soap流 *

* * @Date 2017年7月28日上午11:14:41
* @param soapParam * SoapParam对象实例 * @return * @throws Exception */ private static InputStream getResponseForInputStream(SoapParam soapParam) throws Exception { String requestBody = SoapRequestBody.requestBody(soapParam); logger.info("请求报文:{}", new String[] { requestBody }); URL url = null; InputStream is = null; url = new URL(soapParam.getWsdlLocation()); URLConnection conn = url.openConnection(); conn.setUseCaches(false);// POST请求 禁用缓存 conn.setReadTimeout(6000);// 设置请求时间 conn.setDoInput(true);// 设置从conn读入 conn.setDoOutput(true);// 设置从conn输出 conn.setRequestProperty("Content-Length", Integer.toString(requestBody.length())); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); conn.setRequestProperty( "SOAPAction", String.valueOf(soapParam.getInterfaceName() + soapParam.getCallMethodName())); OutputStream os = conn.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8"); osw.write(requestBody); osw.flush(); osw.close(); is = conn.getInputStream(); return is; } }
SoapRequestBody.java
/**
 * 

* 描述: *

* @package :com.andy.demo.soap
* @author :wanglongjie
*/ package com.andy.demo.soap; import java.util.List; /** *

* 描述:封装SOAP 请求报文 *

* * @author wanglongjie
* @version v1.0 2017年7月28日上午10:43:47 */ public class SoapRequestBody { /** * *

* 描述:封装Soap请求报文 *

* * @Date 2017年7月28日上午10:46:18
* @param soapParam * SoapParam对象实例 * @return */ public static final String requestBody(SoapParam soapParam) { if (validateSoapParam(soapParam)) { throw new IllegalArgumentException("访问 WSDL地址、接口名、方法名不能为空!"); } // 请求报文 StringBuffer body = new StringBuffer(16); // 头部分 body.append(" " + " " + " "); // 中间部分 String methodName = soapParam.getCallMethodName(); String interfaceName = soapParam.getInterfaceName(); // 中间部分-接口、方法开始 body.append("<").append(methodName.trim()); body.append(" xmlns=\"").append(interfaceName.trim()).append("\">"); // 中间部分-参数 List paramNames = soapParam.getMethodParamNames(); List paramValues = soapParam.getMethodParamValues(); if (listIsNull(paramNames)) { // 没用定义参数名称,则使用默认的 arg if (!listIsNull(paramValues)) { String defaultParamName = "arg"; for (int index = 0; index < paramValues.size(); index++) { body.append("<").append(defaultParamName).append(index) .append(">"); body.append(paramValues.get(index)); body.append(" "); } } } else { // 不为空,检查 paramNames 和 paramValues 个数是否保持一致 if (!listIsNull(paramValues)) { if (paramNames.size() != paramValues.size()) { throw new IllegalArgumentException("访问方法的参数名称与参数值 个数不一致!"); } for (int index = 0; index < paramValues.size(); index++) { body.append("<").append(paramNames.get(index)).append(">"); body.append(paramValues.get(index)); body.append(" "); } } } // 中间部分-接口、方法结束 body.append(" "); // 尾部分 body.append(""); return body.toString(); } /** * *

* 描述:验证 SOAP相关参数 是否为空 *

* * @Date 2017年7月28日上午10:51:50
* @param soapParam * SOAP相关参数对象 * @return */ private static boolean validateSoapParam(SoapParam soapParam) { return isNull(soapParam.getWsdlLocation()) && isNull(soapParam.getInterfaceName()) && isNull(soapParam.getCallMethodName()); } /** * *

* 描述:非空判断 *

* * @Date 2017年7月28日上午10:50:09
* @param param * String对象 * @return */ private static boolean isNull(String param) { return param == null || param.trim().length() == 0; } /** * *

* 描述:判断List是否为空 *

* * @Date 2017年7月31日上午10:46:52
* @param list * @return */ private static boolean listIsNull(List list) { return list == null || list.size() == 0; } } SoapParam.java
/**
 * 

* 描述: *

* @package :com.andy.demo.soap
* @author :wanglongjie
*/ package com.andy.demo.soap; import java.util.List; /** *

* 描述:SOAP 相关参数 *

* * @author wanglongjie
* @version v1.0 2017年7月28日上午10:25:23 */ public class SoapParam { /** * WSDL 发布地址 */ private String wsdlLocation; /** * 调用方法名称 */ private String callMethodName; /** * 调用方法实参 属性名字符串,最终转化到 methodParamNames 中 */ private String paramName; /** * 调用方法实参 值集合字符串,最终转化到 methodParamValues 中 */ private String paramValues; /** * 调用方法实参 属性名(为空,则默认为 arg0,arg1,arg2...) */ private List methodParamNames; /** * 调用方法实参 值集合(,若methodParamNames不为空,则methodParamValues 与 methodParamNames * 个数保持一致) */ private List methodParamValues; /** * 服务名称 */ private String interfaceName; /** * */ public SoapParam() { super(); } /** * @param wsdlLocation * WSDL 发布地址 * @param callMethodName * 调用方法名称 * @param interfaceName * 服务名称 */ public SoapParam(String wsdlLocation, String callMethodName, String interfaceName) { super(); this.wsdlLocation = wsdlLocation; this.callMethodName = callMethodName; this.interfaceName = interfaceName; } /** * @return the wsdlLocation */ public String getWsdlLocation() { return wsdlLocation; } /** * @param wsdlLocation * the wsdlLocation to set */ public void setWsdlLocation(String wsdlLocation) { this.wsdlLocation = wsdlLocation; } /** * @return the callMethodName */ public String getCallMethodName() { return callMethodName; } /** * @param callMethodName * the callMethodName to set */ public void setCallMethodName(String callMethodName) { this.callMethodName = callMethodName; } /** * @return the interfaceName */ public String getInterfaceName() { return interfaceName; } /** * @param interfaceName * the interfaceName to set */ public void setInterfaceName(String interfaceName) { this.interfaceName = interfaceName; } /** * @return the methodParamNames */ public List getMethodParamNames() { return methodParamNames; } /** * @param methodParamNames * the methodParamNames to set */ public void setMethodParamNames(List methodParamNames) { this.methodParamNames = methodParamNames; } /** * @return the methodParamValues */ public List getMethodParamValues() { return methodParamValues; } /** * @param methodParamValues * the methodParamValues to set */ public void setMethodParamValues(List methodParamValues) { this.methodParamValues = methodParamValues; } /** * @return the paramName */ public String getParamName() { return paramName; } /** * @param paramName * the paramName to set */ public void setParamName(String paramName) { this.paramName = paramName; } /** * @return the paramValues */ public String getParamValues() { return paramValues; } /** * @param paramValues * the paramValues to set */ public void setParamValues(String paramValues) { this.paramValues = paramValues; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "SoapParam [wsdlLocation=" + wsdlLocation + ", callMethodName=" + callMethodName + ", methodParamNames=" + methodParamNames + ", methodParamValues=" + methodParamValues + ", interfaceName=" + interfaceName + "]"; } }
网友评论