调用Web Service的方法
调用 Web Service 示例
1. 用java写一个应用程序,如:(Calculator.java)
publicclassCalculator{
publicintadd(inti,intj)
{
returni+j;
}
publicintsubstract(inti,intj)
{
returni-j;
}
publicintadd(inti,intj)
{
returni+j;
}
publicintsubstract(inti,intj)
{
returni-j;
}
1. 放在axis下,更名为Calculator.jws
2. 利用axis生成WSDL,并复制IE中的URL,下面将用到。
3. 调用web服务
1) 用.net调用
新建一个项目(WinForm,ASP.net都可以)。在起始页面上放置一个文本输入框用来显示调用Web Services的结果,放置一个按钮,用来单击调用Web Services。然后,选择添加Web 引用,在WSDL一栏中把刚才得到的WSDL地址复制过来,Web 引用的名称输入JavaService,单击添加引用按钮就可以了。此时,我们可以在VS.net 的Solution Explore中看到这个Web 引用。
在按钮的单击事件中输入下列代码:
inti=int.Parse(textBox2.Text.ToString());
intj=int.Parse(textBox3.Text.ToString());
JavaService.CalculatorServicejs=newJavaService.CalculatorService();
textBox1.Text=js.add(i,j).ToString();
intj=int.Parse(textBox3.Text.ToString());
JavaService.CalculatorServicejs=newJavaService.CalculatorService();
textBox1.Text=js.add(i,j).ToString();
其中JavaService是引用Web服务是自己定义的名字:
CalculatorService是wsdl中Service的name:
1) 用java调用Wenservice
a) 首先新建一个类(TestNetService.Java)
b) 将axis中WEB-INF/lib中的所有jar文件添加到lib引用中
c) 在新建类中添加以下代码:






































d) 注意:
其中String endpoint="http://localhost:8080/axis/Calculator.jws";URL是本地的jws文件
call.setOperationName(new QName("http://www.my.com/SU","add"));中的add是应用程序中的函数名,或者说是WSDL中operation的name的值。
这样,一个.NET客户端就完成了,测试一下,工作正常。
本文地址:http://www.45fan.com/a/question/70184.html