Axis2開發WebService客戶端——RPC方式
package client;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
/**
* 在建立QName物件時,QName類的構造方法的第一個引數表示WSDL檔案的名稱空間名,
* 即<wsdl:definitions>元素的targetNamespace屬性值,第二個引數指定方法名。
*/
/**
* invokeBlocking方法有三個引數
* 第一個引數的型別為QName物件,表示呼叫的WebService方法名;
* 第二個引數的型別為Object[], 表示呼叫的WebService方法的引數值;
* 第三個引數的型別為Class[],表示呼叫的WebService方法的返回值型別的Class物件。
*/
/**
* 注意: 當方法沒有引數時,invokeBlocking方法的第二個引數值不能是null,而要使用new Object[]{};
* 如果被呼叫的WebService方法沒有返回值,應使用RPCServiceClient類的invokeRobust方法,
* 該方法只有兩個引數,它們的含義與invokeBlocking方法的前兩個引數的含義相同。
*/
public class RPCClient {
public String client(String address, String qName, String methodName, Object[] opAddEntryArgs) {
String ret = null;
try {
// 使用RPC方式呼叫WebService
RPCServiceClient serviceClient = new RPCServiceClient();
// 指定呼叫WebService的URL
EndpointReference targetEPR = new EndpointReference(address);
Options options = serviceClient.getOptions();
options.setTo(targetEPR);
QName opAddEntry = new QName(qName, methodName);
Class[] classes = new Class[] { String.class };
ret = (String) serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0];
System.out.println(ret);
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}