本例使用的版本为是Axis2-1.5.4。需要引入如下jar包。
/lib/axiom-api-1.2.10.jar
/lib/axiom-impl-1.2.10.jar
/lib/axis2-adb-1.5.4.jar
/lib/axis2-kernel-1.5.4.jar
/lib/axis2-transport-http-1.5.4.jar
/lib/axis2-transport-local-1.5.4.jar
/lib/commons-codec-1.3.jar
/lib/commons-httpclient-3.1.jar
/lib/commons-logging-1.1.3.jar
/lib/wsdl4j-1.6.2.jar
/lib/XmlSchema-1.4.3.jar
/lib/neethi-2.0.4.jar
/lib/mail-1.4.jar
/lib/httpcore-4.0.jar
本例使用的远程Web服务为,天气预报服务:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx。案例演示的调用的方法为getSupportCity,查询本天气预报Web Services支持的国内外城市或地区信息。
输入参数:byProvinceName = 指定的洲或国内的省份,若为ALL或空则表示返回全部城市;
返回数据:一个一维字符串数组 String(),结构为:城市名称(城市代码)。
具体操作创建RPCClient
public class RPCClient {
public static void main(String[] args) {
try {
//使用RPC方式调用WebService
RPCServiceClient rpcServiceClient = new RPCServiceClient();
Options options = rpcServiceClient.getOptions();
//指定调用WebService的URL
String address = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
EndpointReference targetEndpoint = new EndpointReference(address);
options.setAction("http://WebXml.com.cn/getSupportCity");
options.setTo(targetEndpoint);
// 如果报错提示Content-Length,请求内容长度
options.setProperty(HTTPConstants.CHUNKED, "false");
//指定要调用的getSupportCity方法及WSDL文件的命名空间
OMFactory omFactory = OMAbstractFactory.getOMFactory();
OMNamespace omNamespace = omFactory.createOMNamespace("http://WebXml.com.cn/", "");
OMElement method = omFactory.createOMElement("getSupportCity", omNamespace);
OMElement param = omFactory.createOMElement("byProvinceName", omNamespace);
//指定getSupportCity方法的参数值
param.setText("ALL");
method.addChild(param);
OMElement result = rpcServiceClient.sendReceive(method);
System.out.println(result.toString());
} catch (AxisFault e) {
e.printStackTrace();
}
}
所需jar的下载地址为:点击下载