package com.vci.ubcs.example.util; 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.axiom.soap.SOAP11Constants; import org.apache.axis2.Constants; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.client.ServiceClient; import org.springframework.util.StringUtils; public class WsClientUtil { /** * axis2调用客户端 * * @param url 请求服务地址 * @param nameSpace 命名空间 * @param method 方法名 * @param tarName 参数名称 * @param xmlData 请求报文 * @param timeOutInMilliSeconds 超时时间 * @return String 类型的响应报恩 */ public static String sendMsg(String url, String nameSpace,String soapAction, String method, String tarName, String xmlData, long timeOutInMilliSeconds) throws Exception { ServiceClient serviceClient = new ServiceClient(); Options option = new Options(); option.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); option.setTransportInProtocol(Constants.TRANSPORT_HTTP); // 值为targetNamespace+methodName if(StringUtils.isEmpty(soapAction)){ soapAction = nameSpace+method; } option.setAction(soapAction); EndpointReference epfs = new EndpointReference(url); option.setTo(epfs); serviceClient.setOptions(option); OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace namespaceOM = fac.createOMNamespace(nameSpace, ""); OMElement element = fac.createOMElement(method, namespaceOM); OMElement theCityCode = fac.createOMElement(tarName, namespaceOM); theCityCode.setText(xmlData); element.addChild(theCityCode); // OMElement theUserID = fac.createOMElement("theUserID ", namespace); // theUserID.setText(""); // element.addChild(theUserID); OMElement resultOM = serviceClient.sendReceive(element); String result = resultOM.getFirstElement().getText(); System.out.println(result); return result; } }