java调用.net的webservice

目录(?)[-]

  1. 一参考文献
  2. 二概述
  3. 三实例
  4. 注意点
 

一.参考文献

1. http://www.cnblogs.com/xuqifa100/archive/2007/12/13/993926.html 使用.net如何发布web service

2.WebService跨语言的问题

3.Java调用DotNet WebService为什么那么难?

4. java调用.net服务例子

5.使用axis调用.net服务端

二.概述

前面写了一篇博客eclipse+webservice 是在java环境下进行的。考虑到webservice的跨系统,跨语言,跨网络的特性,下面写了一个实例来测试其跨语言的的特性。

首先是用asp.net开发一个webservice,然后再java中创建客户端来调用这个service。

三.实例

(1)打开visual studio 2010,新建项目,如下图所示:

新建的项目结果如下图所示:

(2)在Service1.asmx.cs中添加服务方法,代码如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6.   
  7. namespace AspWebService1  
  8. {  
  9.     /// <summary>   
  10.     /// Service1 的摘要说明   
  11.     /// </summary>   
  12.     [WebService(Namespace = "http://erplab.sjtu.edu/")]  
  13.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  14.     [System.ComponentModel.ToolboxItem(false)]  
  15.     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。   
  16.     // [System.Web.Script.Services.ScriptService]   
  17.     public class Service1 : System.Web.Services.WebService  
  18.     {  
  19.   
  20.         [WebMethod]  
  21.         public string HelloWorld()  
  22.         {  
  23.             return "Hello World";  
  24.         }  
  25.   
  26.         [WebMethod]  
  27.         public string sayHelloToPersonNew(String name)  
  28.         {  
  29.             if (name == null)  
  30.             {  
  31.                 name = "nobody";  
  32.             }  
  33.             return "hello," + name;  
  34.         }  
  35.   
  36.         [WebMethod]  
  37.         public double count(double number, double price, double discount)  
  38.         {  
  39.             return number * price * discount;  
  40.         }  
  41.   
  42.           
  43.         [WebMethod]  
  44.         public float getFloat(float x)  
  45.         {  
  46.             return x;  
  47.         }  
  48.   
  49.         //加法   
  50.         [WebMethod]  
  51.         public float plus(float x, float y)  
  52.         {  
  53.             return x + y;  
  54.         }  
  55.   
  56.         //减法   
  57.         [WebMethod]  
  58.         public float minus(float x, float y)  
  59.         {  
  60.             return x - y;  
  61.         }  
  62.   
  63.         //乘法   
  64.         [WebMethod]  
  65.         public float multiply(float x, float y)  
  66.         {  
  67.             return x * y;  
  68.         }  
  69.   
  70.         //除法   
  71.         [WebMethod]  
  72.         public float divide(float x, float y)  
  73.         {  
  74.             if (y != 0)  
  75.             {  
  76.                 return x / y;  
  77.             }  
  78.             else  
  79.                 return -1;  
  80.         }  
  81.   
  82.     }  
  83. }  
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程序如下所示:

  1. package edu.sjtu.erplab.aspwebservice;  
  2.   
  3. import javax.xml.namespace.QName;  
  4. import javax.xml.rpc.ParameterMode;  
  5. import org.apache.axis.client.Call;  
  6. import org.apache.axis.client.Service;  
  7. import org.apache.axis.encoding.XMLType;  
  8.   
  9. public class AspWebServiceTestClient1 {  
  10.   
  11.     public static void main(String[] args) throws Exception {  
  12.         // 定义方法   
  13.         String method = "HelloWorld";  
  14.         String methodPlus = "plus";  
  15.         String methodMinus = "minus";  
  16.         String methodMultiply = "multiply";  
  17.         String methodDivide = "divide";  
  18.         String methodSayTo = "sayHelloToPersonNew";  
  19.         // 定义服务   
  20.         Service service = new Service();  
  21.   
  22.         // 测试1:调用HelloWorld方法,方法没有参数   
  23.         Call call = (Call) service.createCall();  
  24.         call.setTargetEndpointAddress(new java.net.URL(  
  25.                 "http://localhost:5329/Service1.asmx"));  
  26.         call.setUseSOAPAction(true);  
  27.         // 第一种设置返回值类型为String的方法   
  28.         call.setReturnType(XMLType.SOAP_STRING);  
  29.         call.setOperationName(new QName("http://erplab.sjtu.edu/", method));  
  30.         call.setSOAPActionURI("http://erplab.sjtu.edu/HelloWorld");  
  31.         String retVal1 = (String) call.invoke(new Object[] {});  
  32.         System.out.println(retVal1);  
  33.   
  34.         // 测试2: 调用sayHelloToPersonNew方法,方法有一个参数:name。sayHelloToPersonNew(String name)   
  35.         Call call2 = (Call) service.createCall();  
  36.         call2.setTargetEndpointAddress(new java.net.URL(  
  37.                 "http://localhost:5329/Service1.asmx"));  
  38.         call2.setUseSOAPAction(true);  
  39.         call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",  
  40.                 "string"));  
  41.         // 第二种设置返回值类型为String的方法   
  42.         call2.setOperationName(new QName("http://erplab.sjtu.edu/", methodSayTo));  
  43.         call2.setSOAPActionURI("http://erplab.sjtu.edu/sayHelloToPersonNew");  
  44.         call2.addParameter(new QName("http://erplab.sjtu.edu/""name"),// 这里的name对应参数名称  
  45.                 XMLType.XSD_STRING, ParameterMode.IN);  
  46.         String retVal2 = (String) call2  
  47.                 .invoke(new Object[] { "asp webservice" });  
  48.         System.out.println(retVal2);  
  49.   
  50.         // 测试3: 调用sgetFloat方法,方法有一个参数:x,为float类型   
  51.         Call call3 = (Call) service.createCall();  
  52.         call3.setTargetEndpointAddress(new java.net.URL(  
  53.                 "http://localhost:5329/Service1.asmx"));  
  54.         call3.setUseSOAPAction(true);  
  55.         call3.setEncodingStyle(null);// 必须有,否为会系统报错。最关键的语句。决定生成xmlns的中soap的命名空间   
  56.         // 第一种设置返回值类型为Float类型的方法   
  57.         call3.setReturnType(org.apache.axis.encoding.XMLType.XSD_FLOAT);  
  58.         call3.setOperationName(new QName("http://erplab.sjtu.edu/""getFloat"));  
  59.         call3.setSOAPActionURI("http://erplab.sjtu.edu/getFloat");  
  60.         call3.addParameter(new QName("http://erplab.sjtu.edu/""x"),// 这里的x对应参数名称  
  61.                 XMLType.XSD_FLOAT, ParameterMode.INOUT);  
  62.         Float retVal3 = (Float) call3.invoke(new Object[] { 123 });  
  63.         System.out.println(retVal3);  
  64.   
  65.         // 测试4: 调用plus方法,方法有两个参数:x,y。plus(float x, float y)   
  66.         Call call4 = (Call) service.createCall();  
  67.         call4.setTargetEndpointAddress(new java.net.URL(  
  68.                 "http://localhost:5329/Service1.asmx"));  
  69.         call4.setUseSOAPAction(true);  
  70.         call4.setEncodingStyle(null);  
  71.         // 第二种设置返回值类型为Float类型的方法   
  72.         call4.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",  
  73.                 "float"));  
  74.         call4.setOperationName(new QName("http://erplab.sjtu.edu/", methodPlus));// 加法  
  75.         call4.setSOAPActionURI("http://erplab.sjtu.edu/plus");  
  76.         call4.addParameter(new QName("http://erplab.sjtu.edu/""x"),// 参数x  
  77.                 org.apache.axis.encoding.XMLType.XSD_FLOAT, ParameterMode.IN);  
  78.         call4.addParameter(new QName("http://erplab.sjtu.edu/""y"),// 参数y  
  79.                 XMLType.XSD_FLOAT, ParameterMode.IN);  
  80.         Float retVal4 = (Float) call4.invoke(new Object[] { 5, 6 });  
  81.         System.out.println(retVal4);  
  82.     }  
  83. }  
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);
	}
}
运行结果:
  1. - Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.  
  2. Hello World  
  3. hello,asp webservice  
  4. 123.0  
  5. 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)设置返回值类型有两种方式:

一种是

  1. call.setReturnType(XMLType.SOAP_STRING);  
call.setReturnType(XMLType.SOAP_STRING);
另外一种是
  1. call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));  
call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));
这两种方法是等价的
出处:http://blog.csdn.net/xw13106209/article/details/7067495
原文地址:https://www.cnblogs.com/mq0036/p/3553999.html