java调用net webservice_java调用.net的webservice

一.参考文献

二.概述

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

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

三.实例

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

0_1323777961ht7T.gif

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

0_1323778013yg5D.gif

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

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Web;

usingSystem.Web.Services;

namespaceAspWebService1

{

/// 

/// Service1 的摘要说明

/// 

[WebService(Namespace ="http://erplab.sjtu.edu/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。

// [System.Web.Script.Services.ScriptService]

publicclassService1 : System.Web.Services.WebService

{

[WebMethod]

publicstringHelloWorld()

{

return"Hello World";

}

[WebMethod]

publicstringsayHelloToPersonNew(String name)

{

if(name ==null)

{

name ="nobody";

}

return"hello,"+ name;

}

[WebMethod]

publicdoublecount(doublenumber,doubleprice,doublediscount)

{

returnnumber * price * discount;

}

[WebMethod]

publicfloatgetFloat(floatx)

{

returnx;

}

//加法

[WebMethod]

publicfloatplus(floatx,floaty)

{

returnx + y;

}

//减法

[WebMethod]

publicfloatminus(floatx,floaty)

{

returnx - y;

}

//乘法

[WebMethod]

publicfloatmultiply(floatx,floaty)

{

returnx * y;

}

//除法

[WebMethod]

publicfloatdivide(floatx,floaty)

{

if(y != 0)

{

returnx / y;

}

else

return-1;

}

}

}

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Services;

namespace AspWebService1

{

///

/// Service1 的摘要说明

///

[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,如下图所示:

0_13237781701qfF.gif

上图中显示的就是我们在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;

publicclassAspWebServiceTestClient1 {

publicstaticvoidmain(String[] args) throws Exception {

// 定义方法

String method ="HelloWorld";

String methodPlus ="plus";

String methodMinus ="minus";

String methodMultiply ="multiply";

String methodDivide ="divide";

String methodSayTo ="sayHelloToPersonNew";

// 定义服务

Service service =newService();

// 测试1:调用HelloWorld方法,方法没有参数

Call call = (Call) service.createCall();

call.setTargetEndpointAddress(newjava.net.URL(

"http://localhost:5329/Service1.asmx"));

call.setUseSOAPAction(true);

// 第一种设置返回值类型为String的方法

call.setReturnType(XMLType.SOAP_STRING);

call.setOperationName(newQName("http://erplab.sjtu.edu/", method));

call.setSOAPActionURI("http://erplab.sjtu.edu/HelloWorld");

String retVal1 = (String) call.invoke(newObject[] {});

System.out.println(retVal1);

// 测试2: 调用sayHelloToPersonNew方法,方法有一个参数:name。sayHelloToPersonNew(String name)

Call call2 = (Call) service.createCall();

call2.setTargetEndpointAddress(newjava.net.URL(

"http://localhost:5329/Service1.asmx"));

call2.setUseSOAPAction(true);

call2.setReturnType(newQName("http://www.w3.org/2001/XMLSchema",

"string"));

// 第二种设置返回值类型为String的方法

call2.setOperationName(newQName("http://erplab.sjtu.edu/", methodSayTo));

call2.setSOAPActionURI("http://erplab.sjtu.edu/sayHelloToPersonNew");

call2.addParameter(newQName("http://erplab.sjtu.edu/","name"),// 这里的name对应参数名称

XMLType.XSD_STRING, ParameterMode.IN);

String retVal2 = (String) call2

.invoke(newObject[] {"asp webservice"});

System.out.println(retVal2);

// 测试3: 调用sgetFloat方法,方法有一个参数:x,为float类型

Call call3 = (Call) service.createCall();

call3.setTargetEndpointAddress(newjava.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(newQName("http://erplab.sjtu.edu/","getFloat"));

call3.setSOAPActionURI("http://erplab.sjtu.edu/getFloat");

call3.addParameter(newQName("http://erplab.sjtu.edu/","x"),// 这里的x对应参数名称

XMLType.XSD_FLOAT, ParameterMode.INOUT);

Float retVal3 = (Float) call3.invoke(newObject[] { 123 });

System.out.println(retVal3);

// 测试4: 调用plus方法,方法有两个参数:x,y。plus(float x, float y)

Call call4 = (Call) service.createCall();

call4.setTargetEndpointAddress(newjava.net.URL(

"http://localhost:5329/Service1.asmx"));

call4.setUseSOAPAction(true);

call4.setEncodingStyle(null);

// 第二种设置返回值类型为Float类型的方法

call4.setReturnType(newQName("http://www.w3.org/2001/XMLSchema",

"float"));

call4.setOperationName(newQName("http://erplab.sjtu.edu/", methodPlus));// 加法

call4.setSOAPActionURI("http://erplab.sjtu.edu/plus");

call4.addParameter(newQName("http://erplab.sjtu.edu/","x"),// 参数x

org.apache.axis.encoding.XMLType.XSD_FLOAT, ParameterMode.IN);

call4.addParameter(newQName("http://erplab.sjtu.edu/","y"),// 参数y

XMLType.XSD_FLOAT, ParameterMode.IN);

Float retVal4 = (Float) call4.invoke(newObject[] { 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 supportisdisabled.

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(newQName("http://www.w3.org/2001/XMLSchema","string"));

call2.setReturnType(new QName("http://www.w3.org/2001/XMLSchema","string"));

这两种方法是等价的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值