Java调用WebService接口的方法

时间:2021-11-04 15:57:15

本文实例讲述了Java调用WebService接口的方法。分享给大家供大家参考。具体如下:

这里讲述有参方法Add,代码如下:

复制代码代码如下:
public static void addTest() {
        try ...{
            Integer i = 1;
            Integer j = 2;
            
            //WebService URL
            String service_url = "http://localhost:4079/ws/Service.asmx";
            
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(service_url));
            
            //设置要调用的方法
            call.setOperationName(new QName("//www.zzvips.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("//www.zzvips.com/Add");
            
            //调用该方法
            Integer res = (Integer)call.invoke(
                    new Object[]...{
                        i, j
                    }
            );
            
            System.out.println( "Result: " + res.toString());
            
        } catch (Exception e) ...{
            System.err.println(e);
        }
}

 

运行,结果返回:Result:3

希望本文所述对大家的Java程序设计有所帮助。