赞
踩
调用wsdl接口的工具类,代码如下:
// An highlighted block package com.core136.util; import com.alibaba.fastjson.JSONObject; import org.activiti.engine.impl.util.json.XML; import org.apache.axiom.om.OMAbstractFactory; import org.apache.axiom.om.OMElement; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.apache.axis2.context.NamedValue; import org.apache.axis2.transport.http.HTTPConstants; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.params.HttpMethodParams; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * @author mcband */ public class WebServerUtils { public static void main(String[] args) throws Exception{ //url地址 String url=""; //命名空间 String targetNamespace=""; //方法名 String methodName=""; //请求参数 List<Map<String,String>> params=new ArrayList<>(); Map<String,String> params0=new LinkedHashMap(); params0.put("year", ""); params0.put("title", ""); params0.put("code",""); params.add(params0); //请求头参数 List<NamedValue> paramHeaders=new ArrayList<NamedValue>(); JSONObject res=sendWsdl(url,targetNamespace,methodName,params,paramHeaders); System.out.println("调用返回结果:"+res); } /*** * 调用Wsdl接口 * @param url 接口地址,可不加?wsdl * @param targetNamespace 命名空间 * @param methodName 方法名 * @param params 参数 * @param paramHeaders 请求头参数 * @return {"head":"S=成功;F=失败","msg":"描述信息","data":"返回数据"} */ public static JSONObject sendWsdl(String url,String targetNamespace,String methodName,List<Map<String,String>> params,List<NamedValue> paramHeaders){ JSONObject res=new JSONObject(); try { String endpoint = url.replace("?wsdl",""); //直接引用远程的wsdl文件 Options options = new Options(); options.setTo(new EndpointReference(endpoint)); //解决可能出现的错误:服务器未能识别 HTTP 头 SOAPAction 的值: 。 options.setExceptionToBeThrownOnSOAPFault(false); options.setAction(targetNamespace+methodName); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMFactory fac = OMAbstractFactory.getOMFactory(); //参数1(uri)=即为wsdl文档的targetNamespace;参数2(prefix)=可不填 OMNamespace omNs = fac.createOMNamespace(targetNamespace,""); //方法名 OMElement method = fac.createOMElement(methodName,omNs); //设置参数 if(params!=null && params.size()>0){ for(int i=0;i<params.size();i++){ Map<String,String> map=params.get(i); for(Map.Entry<String, String> entry : map.entrySet()){ String mapKey = entry.getKey(); String mapValue = entry.getValue(); //参数名 OMElement in = fac.createOMElement(mapKey, omNs); //参数值 in.setText(mapValue); method.addChild(in); } } } //设置请求头参数 List<NamedValue> headers = (List<NamedValue>) sender.getOptions().getProperty(HTTPConstants.HTTP_HEADERS); if (headers == null){ headers = new ArrayList<NamedValue>(); } if(paramHeaders!=null && paramHeaders.size()>0){ headers.addAll(paramHeaders); } //====================== 设置重新请求次数 ======================= HttpMethodParams methodParams = new HttpMethodParams(); DefaultHttpMethodRetryHandler retryHandler = new DefaultHttpMethodRetryHandler(0, false); methodParams.setParameter(HttpMethodParams.RETRY_HANDLER, retryHandler); options.setProperty(HTTPConstants.HTTP_METHOD_PARAMS, methodParams); //---------------------------- end ---------------------------- sender.getOptions().setProperty(HTTPConstants.HTTP_HEADERS, headers); OMElement resultEle = sender.sendReceive(method); System.out.println("调用wsdl接口结果:"+resultEle); String result =resultEle.toString(); //xml转成json格式 org.activiti.engine.impl.util.json.JSONObject jSONObject= XML.toJSONObject(result); //设置返回数据 res.put("head","S"); res.put("msg","调用wsdl接口成功"); res.put("data",JSONObject.parseObject(jSONObject.toString())); }catch (Exception e) { e.printStackTrace(); res.put("head","F"); res.put("msg","调用wsdl接口失败:"+ e.getMessage()); } return res; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。