axis2 客户端调用服务器端

本文介绍了使用Apache Axis2库通过三种不同方法实现客户端调用WebService的具体实现过程。包括了使用传统OMElement构建请求、RPCServiceClient进行调用以及AXIOMClient的方式。每种方法都详细展示了如何设置目标URL、请求参数以及处理返回结果。

//方法一:
package com.abin.lir.axis2.client;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class UserClient {
	public static void main(String[] args) {
		try { 
			ServiceClient sc = new ServiceClient(); 
			Options opts = new Options(); 
			opts.setTo(new EndpointReference("http://localhost:9090/universal/services/play")); 
			opts.setAction("urn:echo"); 
			opts.setTimeOutInMilliSeconds(10000);
			sc.setOptions(opts); 
			OMElement res = sc.sendReceive(createPayLoad()); 
			System.out.println(res); 
			} catch (AxisFault e) { 
			e.printStackTrace(); 
			} 
	}
	
	public static OMElement createPayLoad(){ 
		OMFactory fac = OMAbstractFactory.getOMFactory(); 
		OMNamespace omNs = fac.createOMNamespace("http://localhost:9090/universal/services/play", "nsl"); 
		OMElement method = fac.createOMElement("getPassengerInfos",omNs); 
		OMElement value = fac.createOMElement("userID",omNs); 
		value.setText("1024"); 
		method.addChild(value); 
		return method; 
		} 
}





//方法二
package com.abin.lir.axis2.client;
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;
public class RPCClient {
	public static void main(String[] args) throws AxisFault {
		  // 使用RPC方式调用WebService         
        RPCServiceClient serviceClient = new RPCServiceClient(); 
        Options options = serviceClient.getOptions(); 
        // 指定调用WebService的URL 
        EndpointReference targetEPR = new EndpointReference( 
                "http://localhost:9090/universal/services/play"); 
        options.setTo(targetEPR); 
        // 指定方法的参数值 
        Object[] requestParam = new Object[] {"1024"}; 
        // 指定方法返回值的数据类型的Class对象 
        Class[] responseParam = new Class[] {String.class}; 
        // 指定要调用的getGreeting方法及WSDL文件的命名空间 
        QName requestMethod = new QName("http://localhost:9090/universal/services/play", "getPassengerInfos"); 
        // 调用方法并输出该方法的返回值 
        try {
			System.out.println(serviceClient.invokeBlocking(requestMethod, requestParam, responseParam)[0]);
		} catch (AxisFault e) {
			e.printStackTrace();
		} 
	}
}




//方法三
package com.abin.lir.axis2.client;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class AXIOMClient {
	private static EndpointReference targetEPR = new EndpointReference(
			"http://localhost:9090/universal/services/play");
	public static OMElement getPassengerInfos(String symbol) {
		OMFactory fac = OMAbstractFactory.getOMFactory();
		OMNamespace omNs = fac.createOMNamespace(
				"http://localhost:9090/universal/services/play",
				"tns");
		OMElement method = fac.createOMElement("getPassengerInfos", omNs);
		OMElement value = fac.createOMElement("userID", omNs);
		value.addChild(fac.createOMText(value, symbol));
		method.addChild(value);
		return method;
	}
	public static void main(String[] args) {
		try {
			OMElement getPassenger = getPassengerInfos("1024");
			Options options = new Options();
			options.setTo(targetEPR);
			options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
			ServiceClient sender = new ServiceClient();
			sender.setOptions(options);
			OMElement result = sender.sendReceive(getPassenger);
			String response = result.getFirstElement().getText();
			System.err.println("Current passengers: " + response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



转载地址:http://www.blogjava.net/stevenjohn/archive/2012/11/27.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值