WebService – Client调用(Axis2-RPC)
采用的免费webservice接口:
http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
客户端代码如下:
package com.web.hh.constroller; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import org.apache.axis2.transport.http.HTTPConstants; public class ClientWeatherRPC { /* * 第三种方式,RPC */ public static void main(String[] args) throws AxisFault { //使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // options.setExceptionToBeThrownOnSOAPFault(false); options.setSoapVersionURI(org.apache.axiom.soap.SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI); options.setProperty(HTTPConstants.CHUNKED, "false"); //指定调用WebService的URL EndpointReference targetEPR = new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl"); options.setTo(targetEPR); options.setAction("http://WebXml.com.cn/getWeather"); //指定方法的参数值 Object[] opAddEntryArgs = new Object[] {"昌平",""}; //指定要调用的方法及WSDL文件的命名空间 QName opAddEntry = new QName("http://WebXml.com.cn/", "getWeather"); //调用法并输出该方法的返回值 // System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs)); Object[] result=serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, new Class[]{String.class}); for (Object object : result) { System.out.println(object.toString()); } } }
运行抛异常:
十一月 02, 2017 2:04:58 下午 org.apache.axis2.deployment.ModuleDeployer deploy 信息: Deploying module: addressing-1.6.2 - file:/F:/soft-install-soft/MavenRep/org/apache/axis2/axis2/1.6.2/axis2-1.6.2.jar Exception in thread "main" org.apache.axis2.AxisFault: 服务器无法处理请求。 ---> 值不能为空。 参数名: input at org.apache.axis2.util.Utils.getInboundFaultFromMessageContext(Utils.java:531) at org.apache.axis2.description.OutInAxisOperationClient.handleResponse(OutInAxisOperation.java:375) at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:421) at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229) at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165) at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:555) at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:531) at org.apache.axis2.rpc.client.RPCServiceClient.invokeBlocking(RPCServiceClient.java:102) at com.web.hh.constroller.ClientWeatherRPC.main(ClientWeatherRPC.java:34)
但是使用Document方式是可以争取获取数据的,这是为什么?
我们查看其wsdl
style=”document” ! 也就是说,该服务下不支持RPC方式(style=”rpc”则可以)!
故,在客户端调用webservice时,需要看wsdl中是否支持RPC方式!
发表评论