Java调用WebService接口的方法
本文实例讲述了Java调用WebService接口的方法。分享给大家供大家参考。具体如下:
这里讲述有参方法Add,代码如下:
publicstaticvoidaddTest(){
try...{
Integeri=1;
Integerj=2;
//WebServiceURL
Stringservice_url="http://localhost:4079/ws/Service.asmx";
Serviceservice=newService();
Callcall=(Call)service.createCall();
call.setTargetEndpointAddress(newjava.net.URL(service_url));
//设置要调用的方法
call.setOperationName(newQName("https://www.nhooo.com/T","Add"));
//该方法需要的参数
call.addParameter("a",org.apache.axis.encoding.XMLType.XSD_INT,
javax.xml.rpc.ParameterMode.IN);
call.addParameter("b",org.apache.axis.encoding.XMLType.XSD_INT,
javax.xml.rpc.ParameterMode.IN);
//方法的返回值类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);
call.setUseSOAPAction(true);
call.setSOAPActionURI("https://www.nhooo.com/Add");
//调用该方法
Integerres=(Integer)call.invoke(
newObject[]...{
i,j
}
);
System.out.println("Result:"+res.toString());
}catch(Exceptione)...{
System.err.println(e);
}
}
运行,结果返回:Result:3
希望本文所述对大家的Java程序设计有所帮助。