Java调用WebService接口的方法

前端技术 2023/09/07 Java

本文实例讲述了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(\"http://www.phpstudy.net/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(\"http://www.phpstudy.net/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程序设计有所帮助。

本文地址:https://www.stayed.cn/item/18674

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。