// 方式一:產生用戶端代碼調用方式。(使用此方法可以簡化很多代碼)
//
// 通過外掛程式工具產生用戶端代碼進行調用。例如:存在一服務為
// http://127.0.0.1:8000/axis2/services/HelloWorld?wsdl
//儲存為HelloWorld.wsdl.
自動產生了代碼,包括 HelloWorldProxy.java
public static void main(String[] args){
UploadProxy up = new UploadProxy();
up.setEndpoint("http://127.0.0.1:70/uploadImg/services/UploadImg");
//圖片上傳fileinputstream省略
up.updateImage("", uploadBuffer, 0, 0, "", "", "", "");}
// 通過外掛程式可以產生SMSSendServiceStub.java。調用的用戶端代碼如:
public static void main(String[] args) throws Exception {
HelloWorldStub stub;
stub = new HelloWorldStub();
HelloWorldStub.GetName helloWorld = new HelloWorldStub.GetName();//
helloWorld.setName("peipan");
System.out.println(stub.getName(helloWorld).get_return());
}
// 方式二:使用axis2.rpc.client.RPCServiceClient方式調用。
// public static void main(String[] args) throws Exception {
// GetWSByAxis2 ws = new GetWSByAxis2();
// ws.WSUrl = "http://127.0.0.1:8000/axis2/services/HelloWorld";
// String result;
// try {
// result = ws.getStr("getName", "peipan");
// System.out.println(result);
// } catch (AxisFault e) {
// e.printStackTrace();
// }
// }
-------------------------------------------------
package test;
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 GetWSByAxis2 {
private static String EndPointUrl;
private static String QUrl="http://ws.apache.org/axis2";
private QName opAddEntry;
public String WSUrl;
public RPCServiceClient setOption() throws AxisFault
{
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(WSUrl);
options.setTo(targetEPR);
return serviceClient;
}
public QName getQname(String Option){
return new QName (QUrl,Option);
}
//返回String
public String getStr(String Option,String why) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
opAddEntry =this.getQname(Option);
String str = (String) serviceClient.invokeBlocking(opAddEntry,
new Object[]{why}, new Class[]{String.class })[0];
return str;
}
// 返回一維String數組
public String[] getArray(String Option) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
opAddEntry =this.getQname(Option);
String[] strArray = (String[]) serviceClient.invokeBlocking(opAddEntry,
new Object[]{}, new Class[]{String[].class })[0];
return strArray;
}
//從WebService中返回一個對象的執行個體
public Object getObject(String Option,Object o) throws AxisFault
{
RPCServiceClient serviceClient =this.setOption();
QName qname=this.getQname(Option);
Object object = serviceClient.invokeBlocking(qname, new Object[]{},new Class[]{o.getClass()})[0];
return object;
}
///////////////////////////////////////// 讀者可以自己封裝資料類型,如int,byte,float等資料類型
}