package example.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.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class TestClient {
// targetEPR指定打包的Service(.aar文件)在容器中的物理位置。
private static EndpointReference targetEPR=new EndpointReference
("http://localhost:8080/axis2/services/HelloWorld");
public static OMElement getSayHelloOMElement(){
//創(chuàng)建request SOAP包。
OMFactory fac=OMAbstractFactory.getOMFactory();
// OMNamespace指定此SOAP文檔名稱空間。
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
//創(chuàng)建元素sayHello,并指定其在omNs指代的名稱空間中。
OMElement method=fac.createOMElement("sayHello",omNs);
//指定元素的文本內(nèi)容。
method.setText("ZJ");
return method;
}
public static void main(String[] args){
try{
Options options=new Options();
options.setTo(targetEPR);
ServiceClient sender=new ServiceClient();
sender.setOptions(options);
OMElement sayHello=TestClient.getSayHelloOMElement();
//發(fā)出request SOAP,
//同時(shí)將得到的遠(yuǎn)端由sayHello方法返回的信息保存到result。
//通過services.xml能準(zhǔn)確找到sayHello方法所在的文件。
OMElement result=sender.sendReceive(sayHello);
}
catch(Exception axisFault){
axisFault.printStackTrace();
}
}
}
|