一、引用jar包。
完整包路径:http://files.cnblogs.com/files/chenghu/axis完整jar包.rar
二、java程序代码如下所示:
package edu.sjtu.erplab.aspwebservice; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; public class AspWebServiceTestClient1 { public static void main(String[] args) throws Exception { // 定义方法 String method = "HelloWorld"; String methodPlus = "plus"; String methodMinus = "minus"; String methodMultiply = "multiply"; String methodDivide = "divide"; String methodSayTo = "sayHelloToPersonNew"; // 定义服务 Service service = new Service(); // 测试1:调用HelloWorld方法,方法没有参数 Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL( "http://localhost:5329/Service1.asmx")); call.setUseSOAPAction(true); // 第一种设置返回值类型为String的方法 call.setReturnType(XMLType.SOAP_STRING); call.setOperationName(new QName("http://erplab.sjtu.edu/", method)); call.setSOAPActionURI("http://erplab.sjtu.edu/HelloWorld"); String retVal1 = (String) call.invoke(new Object[] {}); System.out.println(retVal1); // 测试2: 调用sayHelloToPersonNew方法,方法有一个参数:name。sayHelloToPersonNew(String name) Call call2 = (Call) service.createCall(); call2.setTargetEndpointAddress(new java.net.URL( "http://localhost:5329/Service1.asmx")); call2.setUseSOAPAction(true); call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema", "string")); // 第二种设置返回值类型为String的方法 call2.setOperationName(new QName("http://erplab.sjtu.edu/", methodSayTo)); call2.setSOAPActionURI("http://erplab.sjtu.edu/sayHelloToPersonNew"); call2.addParameter(new QName("http://erplab.sjtu.edu/", "name"),// 这里的name对应参数名称 XMLType.XSD_STRING, ParameterMode.IN); String retVal2 = (String) call2 .invoke(new Object[] { "asp webservice" }); System.out.println(retVal2); // 测试3: 调用sgetFloat方法,方法有一个参数:x,为float类型 Call call3 = (Call) service.createCall(); call3.setTargetEndpointAddress(new java.net.URL( "http://localhost:5329/Service1.asmx")); call3.setUseSOAPAction(true); call3.setEncodingStyle(null);// 必须有,否为会系统报错。最关键的语句。决定生成xmlns的中soap的命名空间 // 第一种设置返回值类型为Float类型的方法 call3.setReturnType(org.apache.axis.encoding.XMLType.XSD_FLOAT); call3.setOperationName(new QName("http://erplab.sjtu.edu/", "getFloat")); call3.setSOAPActionURI("http://erplab.sjtu.edu/getFloat"); call3.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 这里的x对应参数名称 XMLType.XSD_FLOAT, ParameterMode.INOUT); Float retVal3 = (Float) call3.invoke(new Object[] { 123 }); System.out.println(retVal3); // 测试4: 调用plus方法,方法有两个参数:x,y。plus(float x, float y) Call call4 = (Call) service.createCall(); call4.setTargetEndpointAddress(new java.net.URL( "http://localhost:5329/Service1.asmx")); call4.setUseSOAPAction(true); call4.setEncodingStyle(null); // 第二种设置返回值类型为Float类型的方法 call4.setReturnType(new QName("http://www.w3.org/2001/XMLSchema", "float")); call4.setOperationName(new QName("http://erplab.sjtu.edu/", methodPlus));// 加法 call4.setSOAPActionURI("http://erplab.sjtu.edu/plus"); call4.addParameter(new QName("http://erplab.sjtu.edu/", "x"),// 参数x org.apache.axis.encoding.XMLType.XSD_FLOAT, ParameterMode.IN); call4.addParameter(new QName("http://erplab.sjtu.edu/", "y"),// 参数y XMLType.XSD_FLOAT, ParameterMode.IN); Float retVal4 = (Float) call4.invoke(new Object[] { 5, 6 }); System.out.println(retVal4); } }
三、注意点:
(a)我们发现如果参数是String类型的,那么可以不需要设置call的参数 call3.setEncodingStyle(null); 但是如果传入参数为float类型,那么就必须加上这一条语句。
(b)设置返回值类型有两种方式:
一种是
call.setReturnType(XMLType.SOAP_STRING);
另外一种是
call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));
这两种方法是等价的