首先比如查詢到的一個wsdl文件內容如下:
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://198.168.0.88:6888/ormrpc/services/EASLogin" xmlns:intf="http://198.168.0.88:6888/ormrpc/services/EASLogin" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns1="urn:client" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <!--WSDL created by Apache Axis version: 1.4 Built on Apr 22, 2006 (06:55:48 PDT)--> <wsdl:types> <schema targetNamespace="urn:client" xmlns="http://www.w3.org/2001/XMLSchema"> <import namespace="http://schemas.xmlsoap.org/soap/encoding/"/> <complexType name="WSContext"> <sequence> <element name="dbType" type="xsd:int"/> <element name="dcName" nillable="true" type="xsd:string"/> <element name="password" nillable="true" type="xsd:string"/> <element name="sessionId" nillable="true" type="xsd:string"/> <element name="slnName" nillable="true" type="xsd:string"/> <element name="userName" nillable="true" type="xsd:string"/> </sequence> </complexType> </schema> </wsdl:types> <wsdl:message name="logoutResponse"> <wsdl:part name="logoutReturn" type="xsd:boolean"> </wsdl:part> </wsdl:message> <wsdl:message name="loginResponse"> <wsdl:part name="loginReturn" type="tns1:WSContext"> </wsdl:part> </wsdl:message> <wsdl:message name="loginRequest"> <wsdl:part name="userName" type="xsd:string"> </wsdl:part> <wsdl:part name="password" type="xsd:string"> </wsdl:part> <wsdl:part name="slnName" type="xsd:string"> </wsdl:part> <wsdl:part name="dcName" type="xsd:string"> </wsdl:part> <wsdl:part name="language" type="xsd:string"> </wsdl:part> <wsdl:part name="dbType" type="xsd:int"> </wsdl:part> <wsdl:part name="authPattern" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="loginResponse1"> <wsdl:part name="loginReturn" type="tns1:WSContext"> </wsdl:part> </wsdl:message> <wsdl:message name="logoutRequest"> <wsdl:part name="userName" type="xsd:string"> </wsdl:part> <wsdl:part name="slnName" type="xsd:string"> </wsdl:part> <wsdl:part name="dcName" type="xsd:string"> </wsdl:part> <wsdl:part name="language" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="loginResponse2"> <wsdl:part name="loginReturn" type="tns1:WSContext"> </wsdl:part> </wsdl:message> <wsdl:message name="loginRequest1"> <wsdl:part name="userName" type="xsd:string"> </wsdl:part> <wsdl:part name="password" type="xsd:string"> </wsdl:part> <wsdl:part name="slnName" type="xsd:string"> </wsdl:part> <wsdl:part name="dcName" type="xsd:string"> </wsdl:part> <wsdl:part name="language" type="xsd:string"> </wsdl:part> <wsdl:part name="dbType" type="xsd:int"> </wsdl:part> </wsdl:message> <wsdl:message name="loginByLtpaTokenRequest"> <wsdl:part name="userName" type="xsd:string"> </wsdl:part> <wsdl:part name="ltpaToken" type="xsd:string"> </wsdl:part> <wsdl:part name="slnName" type="xsd:string"> </wsdl:part> <wsdl:part name="dcName" type="xsd:string"> </wsdl:part> <wsdl:part name="language" type="xsd:string"> </wsdl:part> <wsdl:part name="dbType" type="xsd:int"> </wsdl:part> </wsdl:message> <wsdl:message name="loginRequest2"> <wsdl:part name="userName" type="xsd:string"> </wsdl:part> <wsdl:part name="password" type="xsd:string"> </wsdl:part> <wsdl:part name="slnName" type="xsd:string"> </wsdl:part> <wsdl:part name="dcName" type="xsd:string"> </wsdl:part> <wsdl:part name="language" type="xsd:string"> </wsdl:part> <wsdl:part name="dbType" type="xsd:int"> </wsdl:part> <wsdl:part name="authPattern" type="xsd:string"> </wsdl:part> <wsdl:part name="isEncodePwd" type="xsd:int"> </wsdl:part> </wsdl:message> <wsdl:message name="loginByLtpaTokenResponse"> <wsdl:part name="loginByLtpaTokenReturn" type="tns1:WSContext"> </wsdl:part> </wsdl:message> <wsdl:portType name="EASLoginProxy"> <wsdl:operation name="logout" parameterOrder="userName slnName dcName language"> <wsdl:input message="impl:logoutRequest" name="logoutRequest"> </wsdl:input> <wsdl:output message="impl:logoutResponse" name="logoutResponse"> </wsdl:output> </wsdl:operation> <wsdl:operation name="login" parameterOrder="userName password slnName dcName language dbType authPattern"> <wsdl:input message="impl:loginRequest" name="loginRequest"> </wsdl:input> <wsdl:output message="impl:loginResponse" name="loginResponse"> </wsdl:output> </wsdl:operation> <wsdl:operation name="login" parameterOrder="userName password slnName dcName language dbType"> <wsdl:input message="impl:loginRequest1" name="loginRequest1"> </wsdl:input> <wsdl:output message="impl:loginResponse1" name="loginResponse1"> </wsdl:output> </wsdl:operation> <wsdl:operation name="login" parameterOrder="userName password slnName dcName language dbType authPattern isEncodePwd"> <wsdl:input message="impl:loginRequest2" name="loginRequest2"> </wsdl:input> <wsdl:output message="impl:loginResponse2" name="loginResponse2"> </wsdl:output> </wsdl:operation> <wsdl:operation name="loginByLtpaToken" parameterOrder="userName ltpaToken slnName dcName language dbType"> <wsdl:input message="impl:loginByLtpaTokenRequest" name="loginByLtpaTokenRequest"> </wsdl:input> <wsdl:output message="impl:loginByLtpaTokenResponse" name="loginByLtpaTokenResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="EASLoginSoapBinding" type="impl:EASLoginProxy"> <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="logout"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="logoutRequest"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://login.webservice.bos.kingdee.com" use="encoded"/> </wsdl:input> <wsdl:output name="logoutResponse"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="loginRequest"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://login.webservice.bos.kingdee.com" use="encoded"/> </wsdl:input> <wsdl:output name="loginResponse"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="loginRequest1"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://login.webservice.bos.kingdee.com" use="encoded"/> </wsdl:input> <wsdl:output name="loginResponse1"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="login"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="loginRequest2"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://login.webservice.bos.kingdee.com" use="encoded"/> </wsdl:input> <wsdl:output name="loginResponse2"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="loginByLtpaToken"> <wsdlsoap:operation soapAction=""/> <wsdl:input name="loginByLtpaTokenRequest"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://login.webservice.bos.kingdee.com" use="encoded"/> </wsdl:input> <wsdl:output name="loginByLtpaTokenResponse"> <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://198.168.0.88:6888/ormrpc/services/EASLogin" use="encoded"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="EASLoginProxyService"> <wsdl:port binding="impl:EASLoginSoapBinding" name="EASLogin"> <wsdlsoap:address location="http://198.168.0.88:6888/ormrpc/services/EASLogin"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
一般有幾種方式調用此接口:
方式一:通過wsdl文件或者鏈接使用相關IDE軟件生成Java文件
這種方式比較累贅,不做推薦,使用IDE軟甲即可完成
方式二:使用apache的動態代理方式實現
話不多說,直接上代碼:
import java.net.URL; import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class UsingDII { public static void main(String[] args) { try { int a = 100, b=60;
// 對應的targetNamespace String endPoint = "http://198.168.0.88:6888/ormrpc/services/EASLogin"; Service service = new Service(); Call call = (Call)service.createCall(); call.setOperationName(new QName(endPoint,"EASLogin")); call.setTargetEndpointAddress(new URL(endPoint));
// a,b 調用此方法的參數 String result = (String)call.invoke(new Object[]{new Integer(a),new Integer(b)}); System.out.println("result is :"+result); } catch (Exception e) { e.printStackTrace(); } } }
方式三:使用Dynamic Proxy動態代理
import java.net.URL; import javax.xml.*; public class UsingDP { public static void main(String[] args) { try { int a = 100, b=60; String wsdlUrl = "http://198.168.0.88:6888/ormrpc/services/EASLogin?wsdl"; String nameSpaceUri = "http://198.168.0.88:6888/ormrpc/services/EASLogin"; String serviceName = "EASLoginProxyService"; String portName = "EASLogin"; ServiceFactory serviceFactory = ServiceFactory.newInstance(); Service service = serviceFactory.createService(new URL(wsdlUrl),new QName(nameSpaceUri,serviceName));
// 返回值是自己封裝的類 AddFunctionServiceIntf adsIntf = (AddFunctionServiceIntf)service.getPort(new QName(nameSpaceUri,portName),AddFunctionServiceIntf.class); System.out.println("result is :"+adsIntf.addInt(a, b)); } catch (Exception e) { e.printStackTrace(); } } }
方式四:使用httpclient方式
參考:https://www.cnblogs.com/yangchengdebokeyuan/p/11352950.html
直接上代碼:
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
// 這里引得依賴 包的話需要自己找了 下面地址可以找到
//https://mvnrepository.com/
public static InputStream postXmlRequestInputStream(String requestUrl, String xmlData) throws IOException{ PostMethod postMethod = new PostMethod(requestUrl); byte[] b = xmlData.getBytes("utf-8"); InputStream is = new ByteArrayInputStream(b, 0, b.length); RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml;charset=utf-8"); postMethod.setRequestEntity(re); HttpClient httpClient = new HttpClient(); httpClient.getParams().setAuthenticationPreemptive(true); httpClient.getHostConfiguration().setProxy(CommonPptsUtil.get("PROXY_HOST"), Integer.valueOf(CommonPptsUtil.get("PROXY_PORT"))); int statusCode = httpClient.executeMethod(postMethod); logger.debug("responseCode:"+statusCode); if (statusCode != 200) { return null; } return postMethod.getResponseBodyAsStream(); } public static void main(String[] args) { String reqJsonStr = "{\"workId\":\"20171018161622\",\"status\":\"201\",\"startTime\":\"2017-10-18 16:16:22\"}"; String xmlData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.interfacemodule.cic.com/\"><soapenv:Header/><soapenv:Body><ser:statusWriteBack><jsonString>" + "{\"workId\":\"314\",\"orderId\":\"5207675\",\"longitude\":\"104.068310\",\"latitude\":\"30.539503\",\"sendTime\":\"2019-08-13 08:38:45\",\"servicePerName\":\"於xx\",\"servicePerPhone\":\"184xxxx7680\"}" + "</jsonString></ser:statusWriteBack></soapenv:Body></soapenv:Envelope>"; String url = "http://xx.xxx.246.88:7103/avs/services/CCService?wsdl"; SAXReader reader = new SAXReader(); String result = ""; try { InputStream in = postXmlRequestInputStream(url,xmlData); if(in!=null){ Document doc = reader.read(in); result = doc.getRootElement().element("Body").element("statusWriteBackResponse").element("return").getText(); logger.debug("result:"+result); } } catch (Exception e) { logger.error("error:",e); e.printStackTrace(); } }
}
CommonPptsUtil://就是獲取配置文件里的代理信息
import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; import org.apache.log4j.Logger; /** * 通用屬性文件工具類 * * @author y.c * */ public class CommonPptsUtil { private static final Logger logger = Logger.getLogger(CommonPptsUtil.class); private static final String CONFIG_FILE = "common.properties"; private static PropertiesConfiguration ppts; static { try { ppts = new PropertiesConfiguration(CONFIG_FILE); ppts.setReloadingStrategy(new FileChangedReloadingStrategy()); } catch (ConfigurationException e) { logger.error("文件【common.properties】加載失敗!"); ppts = null; } } /** * 獲取屬性值 * * @param key * @return 屬性值 */ public static String get(String key) { if (ppts == null) { return null; } return ppts.getString(key); } }
方式五:使用CXF動態調用webservice接口
代碼如下:參考: https://blog.csdn.net/itdragons/article/details/75390756
package cxfClient; import org.apache.cxf.endpoint.Endpoint; import javax.xml.namespace.QName; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.apache.cxf.service.model.BindingInfo; import org.apache.cxf.service.model.BindingOperationInfo; public class CxfClient { public static void main(String[] args) throws Exception { String url = "http://localhost:9091/Service/SayHello?wsdl"; String method = "say"; Object[] parameters = new Object[]{"我是參數"}; System.out.println(invokeRemoteMethod(url, method, parameters)[0]); } public static Object[] invokeRemoteMethod(String url, String operation, Object[] parameters){ JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); if (!url.endsWith("wsdl")) { url += "?wsdl"; } org.apache.cxf.endpoint.Client client = dcf.createClient(url); //處理webService接口和實現類namespace不同的情況,CXF動態客戶端在處理此問題時,會報No operation was found with the name的異常 Endpoint endpoint = client.getEndpoint(); QName opName = new QName(endpoint.getService().getName().getNamespaceURI(),operation); BindingInfo bindingInfo= endpoint.getEndpointInfo().getBinding(); if(bindingInfo.getOperation(opName) == null){ for(BindingOperationInfo operationInfo : bindingInfo.getOperations()){ if(operation.equals(operationInfo.getName().getLocalPart())){ opName = operationInfo.getName(); break; } } } Object[] res = null; try { res = client.invoke(opName, parameters); } catch (Exception e) { e.printStackTrace(); } return res; } }