如何将SOAP调用封装类改写为支持长尾词的?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1298个文字,预计阅读时间需要6分钟。
java/** * 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;
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 = "
* 描述:调用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(" " + "
本文共计1298个文字,预计阅读时间需要6分钟。
java/** * 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;
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 = "
* 描述:调用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(" " + "
* 描述:验证 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 相关参数 *
* @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

