1.axis1 调用
**首先要引入jar:
<dependency>
<groupId>axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>**
=================================================================================================================
/**
* axis1请求webservice接口
* @param wsdlUrl wsdl服务地址
* @param targetNameSpace 命名空间
* @param parameName 参数名称
* @param soapAction 调用方法
* @param parameValue 参数值 注意参数名称和值要一一对应
* @return
*/
public static String postByAxis(String wsdlUrl,String targetNameSpace,String method,String[]paramName,Object[]parameValue) {
String returnMsg = "";
String returnXml = "";
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));
//方法名称
call.setOperationName(new QName(targetNameSpace,method));
//参数名称
for (String param : paramName) {
call.addParameter(new QName(targetNameSpace,param),XMLType.XSD_STRING, ParameterMode.IN);
}
//启用soap
call.setUseSOAPAction(true);
//返回参数的类型(不能用Array,否则报错)
call.setReturnType(XMLType.XSD_STRING);
//接口中调用方法的soapAction
call.setSOAPActionURI(targetNameSpace+"/"+method);
SOAPService soap = new SOAPService();
//接口中定义的webservice名称
soap.setName("VmsSdkWebService");
call.setSOAPService(soap);
returnMsg = (String) call.invoke(parameValue);
LOGGER.info("returnMsg==>"+returnMsg);
//去掉转义字符
returnXml = StringEscapeUtils.unescapeHtml4(returnMsg);
System.out.println("returnMsg==convert====>"+returnXml);
//获取返回结果
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
return returnXml;
}
================================================================================================
Axis2 调用
axis2-adb-1.6.2.jar;
axis2-kernel-1.6.2.jar
axis2-transport-http-1.6.2.jar
axis2-transport-local-1.6.2.jar
导入的类:
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
================================================================================================
/**
* Axis2请求webservice接口
* @param wsdlUrl
* @param targetNameSpace
* @param parameName
* @param soapAction
* @param parameValue
* @return
*/
public static String excute_axis_2(String wsdlUrl,String targetNameSpace,String method,Object[]parameValue) {
RPCServiceClient client;
Options options;
String results ="";
try {
client = new RPCServiceClient();
options = client.getOptions();
options.setTo(new EndpointReference(wsdlUrl));
options.setTimeOutInMilliSeconds(1000 * 60 * 5);// 毫秒单位
options.setAction(method);
Object[] response = client.invokeBlocking(new QName(targetNameSpace, method), parameValue, new Class[]{String.class});
results = (String) response[0];
} catch (AxisFault e) {
e.printStackTrace();
}
return results;
}
这里的方法axis2 较于 axis可以不用传字段名称,只需要按接口入参顺序设置入参的值。