日期:2014-05-17 浏览次数:20703 次
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 TestAxis2 {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
new TestAxis2().test();
}
public void test() throws Exception {
// 服务器端WebService的WSDL连接串
String serviceUrl = "http://localhost:8080/axis2/services/MyWebService?wsdl" ;
RPCServiceClient serviceClient = null;
String resultString = "";
serviceClient = getServiceClient(serviceUrl);
// 服务器端开放的方法名
String wsFunction = "helloWebService";
System.out.println(wsFunction);
// 要传给服务器开放方法的参数.
String jsonString = "你好" ;
//服务器的返回数据
resultString = login(serviceUrl,serviceClient, jsonString, wsFunction);
System.out.println("resultString=" + resultString);
}
public RPCServiceClient getServiceClient(String wsdlUrl) {
RPCServiceClient serviceClient = null;
try {
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(wsdlUrl);
options.setTo(targetEPR);
} catch (AxisFault e) {
e.printStackTrace();
}
return serviceClient;
}
public String login(String serviceUrl,RPCServiceClient serviceClient, String jsonString, String wsFunction) throws AxisFault {
// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值
QName opLogin = new QName("http://ws.apache.org/axis2", wsFunction);
// 参数,如果有多个,继续往后面增加即可,不用指定参数的名称
Object[]&nb