`
king_tt
  • 浏览: 2125142 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

调用 Web Service 示例

 
阅读更多

调用 Web Service 示例

本文档说明分别用java和.Net调用webservice的方法
1. 用java写一个应用程序,如:(Calculator.java)

publicclassCalculator{
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();


其中JavaService是引用Web服务是自己定义的名字:

CalculatorService是wsdl中Service的name:

1) 用java调用Wenservice
a) 首先新建一个类(TestNetService.Java)
b) 将axis中WEB-INF/lib中的所有jar文件添加到lib引用中
c) 在新建类中添加以下代码:

importjava.util.Date;
importjava.text.DateFormat;
importjava.util.Date;
importjava.text.DateFormat;
importorg.apache.axis.client.Call;
importorg.apache.axis.client.Service;
importjavax.xml.namespace.QName;
importjava.lang.Integer;
importjavax.xml.rpc.ParameterMode;

publicclassTestNetService...{
publicTestNetService()...{
}

publicstaticvoidmain(String[]args)...{
try...{
Integeri
=newInteger(1);
Integerj
=newInteger(2);
Stringendpoint
="http://localhost:8080/axis/Calculator.jws";
Serviceservice
=newService();
Callcall
=(Call)service.createCall();
call.setTargetEndpointAddress(
newjava.net.URL(endpoint));
call.setOperationName(
newQName("http://www.my.com/SU","add"));
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_INT);
call.setUseSOAPAction(
true);
call.setSOAPActionURI(
"http://www.my.com/Rpc");
Integerk
=(Integer)call.invoke(newObject[]...{i,j});
System.out.println(
"resultis"+k.toString()+".");
}

catch(Exceptione)...{System.err.println(e.toString());}
}

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客户端就完成了,测试一下,工作正常。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics