Axis2调用WebService

       本例使用的版本为是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的下载地址为:点击下载


使用 Java Axis2 调用 WebService 接口可以通过以下步骤实现: 1. 下载并安装 Axis2。你可以从官方网站下载安装包,或者从 Maven 中央仓库获取依赖。 2. 创建一个 Java 项目,并将 Axis2 WebService 客户端库添加到项目依赖中。 3. 根据 WebService WSDL 文件生成客户端代码。你可以使用 Axis2 自带的 wsdl2java 工具,在命令行中运行以下命令: ``` wsdl2java -uri <wsdl-url> -p <package-name> -d <output-directory> ``` 其中,`<wsdl-url>` 是 WebService 的 WSDL 文件地址,`<package-name>` 是你想要生成代码的 Java 包名,`<output-directory>` 是生成代码的输出目录。 4. 在你的 Java 代码中创建 WebService 客户端对象,并调用 WebService 方法。例如: ``` MyWebServiceStub stub = new MyWebServiceStub(); MyWebServiceStub.MyWebServiceRequest request = new MyWebServiceStub.MyWebServiceRequest(); request.setParameter("value"); MyWebServiceStub.MyWebServiceResponse response = stub.myWebServiceMethod(request); System.out.println(response.getResult()); ``` 这里的 `MyWebServiceStub` 是在第三步中生成的客户端代码中的类名,`myWebServiceMethod` 是 WebService 的方法名,`MyWebServiceRequest` 和 `MyWebServiceResponse` 是方法的请求和响应对象。 以上就是使用 Java Axis2 调用 WebService 接口的基本步骤。需要注意的是,在实际项目中,你可能还需要处理异常、设置 WebService 的认证和安全等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hoking

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值