1. 用VS.NET新建一个ASP.NET web 服务,命名为MathServices.asmx。代码如下:
using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols;
[WebService(Namespace = "http://localhost/WebSite/MathService/MathServices.asmx", Description = "算术运算( + - * / )服务")] [WebServiceBinding(ConformsTo = WsiProfiles.None)] public class MathServices : System.Web.Services.WebService { public MathServices() {
//如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Add")] [WebMethod(Description = "两单精度数相加")] public float Add(float A, float B) { return A + B; } [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Subtract")] [WebMethod(Description = "两单精度数相减")] public float Subtract(float A, float B) { return A - B; } [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Multiply")] [WebMethod(Description = "两单精度数相乘")] public float Multiply(float A, float B) { return A * B; } [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Divide")] [WebMethod(Description = "两单精度数相除")] public float Divide(float A, float B) { if (B == 0) return -1; return A / B; }
}
在Web.Config中需加上
<system.web> <webServices> <conformanceWarnings> <remove name='BasicProfile1_1'/> </conformanceWarnings> </webServices> </system.web>
2. 打开Eclipse,新建一个项目,添加一个java class ,命名为TestNetService,输入下列代码:
package myWebServiceJavaClient;
import org.apache.axis.client.Call; import org.apache.axis.client.Service; import javax.xml.namespace.QName;
/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2008</p> * <p>Company: </p> * @author XiangWei * @version 1.0 */ public class TestNetService { public TestNetService() { } public static void main(String[] args) { try { Float i = new Float(7); Float j = new Float(3); String endpoint="http://localhost/WebSite/MathService/MathServices.asmx"; Service service = new Service(); Call call = (Call)service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName("http://localhost/WebSite/MathService/MathServices.asmx","Multiply")); call.addParameter("A",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN); call.addParameter("B",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN); call.setReturnType(org.apache.axis.encoding.XMLType.XSD_FLOAT); call.setUseSOAPAction(true); call.setSOAPActionURI("http://localhost/WebSite/MathService/MathServices.asmx/Multiply"); Float k = (Float)call.invoke(new Object[]{i,j}); System.out.println( "result is " + k.toString() + "."); } catch (Exception e) { System.err.println(e.toString()); } } }
遇到的问题: 1.抛出一个SoapAction异常。 在MathServices中的 [web method] 的上一行添加下列代码: [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Add")] [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Subtract")] [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Multiply")] [SoapRpcMethod(Action = "http://localhost/WebSite/MathService/MathServices.asmx/Divide")]
2.测试服务时出现:此 Web 服务不符合 WS-I Basic Profile v1.1 a.并将类的WebServiceBinding 属性中ConformsTo 指定为WsiProfiles.None b.创建一个配置文件,在其中添加 <configuration> <system.web> <webServices> <conformanceWarnings> <remove name='BasicProfile1_1'/> </conformanceWarnings> </webServices> </system.web> </configuration>
3.安装Axis 1.copy axis安装包下webapps文件夹中的axis目录到tomcat的webapps下发布axis应用 2.把axis用到的jar包添加到CLASSPATH环境变量中去。如下: F:/tomcat/webapps/axis/WEB-INF/lib/axis.jar; F:/tomcat/webapps/axis/WEB-INF/lib/jaxrpc.jar; F:/tomcat/webapps/axis/WEB-INF/lib/saaj.jar; F:/tomcat/webapps/axis/WEB-INF/lib/commons-discovery-0.2.jar; F:/tomcat/webapps/axis/WEB-INF/lib/commons-logging-1.0.4.jar; F:/tomcat/webapps/axis/WEB-INF/lib/wsdl4j-1.5.1.jar;