您好,欢迎访问一九零五行业门户网

用的Java调用WebService的

这是一个用java的调用c#版的webservice接口的例子:
c#接口:
<span style="font-size: 11px;"> using system; using system.web; using system.web.services; using system.web.services.protocols; using system.web.services.description; [webservice(namespace = "http://www.tangs.com/")] [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)] public class service : system.web.services.webservice { public service () ...{ //如果使用设计的组件,请取消注释以下行 //initializecomponent(); } [soaprpcmethod(action = "http://www.tangs.com/add", requestnamespace = "http://www.tangs.com/t", responsenamespace = "http://www.tangs.com/t", use = soapbindinguse.literal)] [webmethod] public int add(int a, int b) ...{ return a + b; } [soaprpcmethod(action = "http://www.tangs.com/hello", requestnamespace = "http://www.tangs.com/t", responsenamespace = "http://www.tangs.com/t", use = soapbindinguse.literal)] [webmethod] public string helloworld() ...{ return "hello, world!"; } } ...</span>
java的调用这个web服务中的添加方法和helloworld的方法:
1,有参方法:添加
<span style="font-size: 11px;"> public static void addtest() { try ...{ integer i = 1; integer j = 2; //webservice url string service_url = "http://localhost:4079/ws/service.asmx"; service service = new service(); call call = (call) service.createcall(); call.settargetendpointaddress(new java.net.url(service_url)); //设置要调用的方法 call.setoperationname(new qname("http://www.tangs.com/t", "add")); //该方法需要的参数 call.addparameter("a", org.apache.axis.encoding.xmltype.xsd_int, javax.xml.rpc.parametermode.in); call.addparameter("b", org.apache.axis.encoding.xmltype.xsd_int, javax.xml.rpc.parametermode.in); //方法的返回值类型 call.setreturntype(org.apache.axis.encoding.xmltype.xsd_int); call.setusesoapaction(true); call.setsoapactionuri("http://www.tangs.com/add"); //调用该方法 integer res = (integer)call.invoke( new object[]...{ i, j } ); system.out.println( "result: " + res.tostring()); } catch (exception e) ...{ system.err.println(e); } }... </span>
运行,结果返回:结果:3
2.无参方法:的helloworld
<span style="font-size: 11px;"> public static void hellotest() { try ...{ string endpoint = "http://localhost:4079/ws/service.asmx"; service service = new service(); call call = (call) service.createcall(); call.settargetendpointaddress(new java.net.url(endpoint)); call.setoperationname(new qname("http://www.tangs.com/t", "helloworld")); call.setusesoapaction(true); call.setsoapactionuri("http://www.tangs.com/hello"); string res = (string)call.invoke( new object[]...{ null } ); system.out.println( "result: " + res); } catch (exception e) ...{ system.err.println(e.tostring()); } }... </span>
可以看到,调用无参的web服务和有参的基本相同,不过无参调用时,不需要调用呼叫的addparameter方法和setreturntype方法
执行查询查询结果报道查看:你好,世界!
附件在为web服务依赖的jar包
其它类似信息

推荐信息