一.参考文献
1. http://www.cnblogs.com/xuqifa100/archive/2007/12/13/993926.html 使用.net如何发布web service
3.Java调用DotNet WebService为什么那么难?
二.概述
前面写了一篇博客eclipse+webservice 是在java环境下进行的。考虑到webservice的跨系统,跨语言,跨网络的特性,下面写了一个实例来测试其跨语言的的特性。
首先是用asp.net开发一个webservice,然后再java中创建客户端来调用这个service。
三.实例
(1)打开visual studio 2010,新建项目,如下图所示:
新建的项目结果如下图所示:
(2)在Service1.asmx.cs中添加服务方法,代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- namespace AspWebService1
- {
- /// <summary>
- /// Service1 的摘要说明
- /// </summary>
- [WebService(Namespace = "http://erplab.sjtu.edu/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false)]
- // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
- // [System.Web.Script.Services.ScriptService]
- public class Service1 : System.Web.Services.WebService
- {
- [WebMethod]
- public string HelloWorld()
- {
- return "Hello World";
- }
- [WebMethod]
- public string sayHelloToPersonNew(String name)
- {
- if (name == null)
- {
- name = "nobody";
- }
- return "hello," + name;
- }
- [WebMethod]
- public double count(double number, double price, double discount)
- {
- return number * price * discount;
- }
- [WebMethod]
- public float getFloat(float x)
- {
- return x;
- }
- //加法
- [WebMethod]
- public float plus(float x, float y)
- {
- return x + y;
- }
- //减法
- [WebMethod]
- public float minus(float x, float y)
- {
- return x - y;
- }
- //乘法
- [WebMethod]
- public float multiply(float x, float y)
- {
- return x * y;
- }
- //除法
- [WebMethod]
- public float divide(float x, float y)
- {
- if (y != 0)
- {
- return x / y;
- }
- else
- return -1;
- }
- }
- }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace AspWebService1 { /// <summary> /// Service1 的摘要说明 /// </summary> [WebService(Namespace = "http://erplab.sjtu.edu/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 // [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public string sayHelloToPersonNew(String name) { if (name == null) { name = "nobody"; } return "hello," + name; } [WebMethod] public double count(double number, double price, double discount) { return number * price * discount; } [WebMethod] public float getFloat(float x) { return x; } //加法 [WebMethod] public float plus(float x, float y) { return x + y; } //减法 [WebMethod] public float minus(float x, float y) { return x - y; } //乘法 [WebMethod] public float multiply(float x, float y) { return x * y; } //除法 [WebMethod] public float divide(float x, float y) { if (y != 0) { return x / y; } else return -1; } } }(3)发布服务,按CTRL+F5运行项目,即可打开服务首页:http://localhost:5329/Service1.asmx,如下图所示:
上图中显示的就是我们在Service1.asmx.cs文件中定义的服务方法。点击“服务说明”可以查看webservice的wsdl文件。
(4)编写java客户端来测试webservice,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);
- }
- }
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); } }运行结果:
- - Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
- Hello World
- hello,asp webservice
- 123.0
- 11.0
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled. Hello World hello,asp webservice 123.0 11.0
注意点:
(a)我们发现如果参数是String类型的,那么可以不需要设置call的参数 call3.setEncodingStyle(null); 但是如果传入参数为float类型,那么就必须加上这一条语句。
(b)设置返回值类型有两种方式:
一种是
- call.setReturnType(XMLType.SOAP_STRING);
call.setReturnType(XMLType.SOAP_STRING);另外一种是
- call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));
call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));这两种方法是等价的