- 论坛徽章:
- 0
|
Webservice调用方式:axis,soap详解
转自: [url]http://blog.csdn.net/baiboy4493/archive/2009/03/13/3987526.aspx [/url]
调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis和Soap,soap方式主要是用在websphere下
axis方式调用:
Java代码soap方式调用
调用java生成的webservice
Java代码- 1.import org.apache.soap.util.xml.*;
- 2.
- 3.import org.apache.soap.*;
- 4.
- 5.import org.apache.soap.rpc.*;
- 6.
- 7.
- 8.import java.io.*;
- 9.
- 10.import java.net.*;
- 11.
- 12.import java.util.Vector;
- 13.
- 14.
- 15.public class caService{
- 16.
- 17.public static String getService(String user) {
- 18.
- 19.URL url = null;
- 20.
- 21.try {
- 22.
- 23.url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
- 24.
- 25.} catch (MalformedURLException mue) {
- 26.
- 27.return mue.getMessage();
- 28.
- 29.}
- 30.
- 31.// This is the main SOAP object
- 32.
- 33.Call soapCall = new Call();
- 34.
- 35.// Use SOAP encoding
- 36.
- 37.soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
- 38.
- 39.// This is the remote object we're asking for the price
- 40.
- 41.soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
- 42.
- 43.// This is the name of the method on the above object
- 44.
- 45.soapCall.setMethodName("getUser");
- 46.
- 47.// We need to send the ISBN number as an input parameter to the method
- 48.
- 49.Vector soapParams = new Vector();
- 50.
- 51.
- 52.// name, type, value, encoding style
- 53.
- 54.Parameter isbnParam = new Parameter("userName", String.class, user, null);
- 55.
- 56.soapParams.addElement(isbnParam);
- 57.
- 58.soapCall.setParams(soapParams);
- 59.
- 60.try {
- 61.
- 62.// Invoke the remote method on the object
- 63.
- 64.Response soapResponse = soapCall.invoke(url,"");
- 65.
- 66.// Check to see if there is an error, return "N/A"
- 67.
- 68.if (soapResponse.generatedFault()) {
- 69.
- 70.Fault fault = soapResponse.getFault();
- 71.
- 72.String f = fault.getFaultString();
- 73.
- 74.return f;
- 75.
- 76.} else {
- 77.
- 78.// read result
- 79.
- 80.Parameter soapResult = soapResponse.getReturnValue ();
- 81.
- 82.// get a string from the result
- 83.
- 84.return soapResult.getValue().toString();
- 85.
- 86.}
- 87.
- 88.} catch (SOAPException se) {
- 89.
- 90.return se.getMessage();
- 91.
- 92.}
- 93.
- 94.}
- 95.
- 96.}
- import org.apache.soap.util.xml.*;
- import org.apache.soap.*;
- import org.apache.soap.rpc.*;
- import java.io.*;
- import java.net.*;
- import java.util.Vector;
- public class caService{
- public static String getService(String user) {
- URL url = null;
- try {
- url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
- } catch (MalformedURLException mue) {
- return mue.getMessage();
- }
- // This is the main SOAP object
- Call soapCall = new Call();
- // Use SOAP encoding
- soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
- // This is the remote object we're asking for the price
- soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
- // This is the name of the method on the above object
- soapCall.setMethodName("getUser");
- // We need to send the ISBN number as an input parameter to the method
- Vector soapParams = new Vector();
- // name, type, value, encoding style
- Parameter isbnParam = new Parameter("userName", String.class, user, null);
- soapParams.addElement(isbnParam);
- soapCall.setParams(soapParams);
- try {
- // Invoke the remote method on the object
- Response soapResponse = soapCall.invoke(url,"");
- // Check to see if there is an error, return "N/A"
- if (soapResponse.generatedFault()) {
- Fault fault = soapResponse.getFault();
- String f = fault.getFaultString();
- return f;
- } else {
- // read result
- Parameter soapResult = soapResponse.getReturnValue ();
- // get a string from the result
- return soapResult.getValue().toString();
- }
- } catch (SOAPException se) {
- return se.getMessage();
- }
- }
- }
复制代码 返回一维数组时
Java代码- 1.Parameter soapResult = soapResponse.getReturnValue();
- 2.
- 3.String[] temp = (String[])soapResult.getValue();
- Parameter soapResult = soapResponse.getReturnValue();
- String[] temp = (String[])soapResult.getValue();
复制代码 调用ASP.Net生成的webservice
Java代码- 1.private String HelloWorld(String uri, String u) {
- 2.
- 3.try {
- 4.
- 5.SOAPMappingRegistry smr = new SOAPMappingRegistry();
- 6.
- 7.StringDeserializer sd = new StringDeserializer();
- 8.
- 9.ArraySerializer arraySer = new ArraySerializer();
- 10.
- 11.BeanSerializer beanSer = new BeanSerializer();
- 12.
- 13.smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(
- 14.
- 15."http://tempuri.org/", "HelloWorldResult"), String.class,
- 16.
- 17.null, sd);
- 18.
- 19.smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(
- 20.
- 21."http://tempuri.org/", "temp"), String.class,
- 22.
- 23.beanSer, beanSer);
- 24.
- 25.
- 26.URL url = new URL(uri);
- 27.
- 28.Call call = new Call();
- 29.
- 30.call.setSOAPMappingRegistry(smr);
- 31.
- 32.call.setTargetObjectURI("urn:xmethods-Service1");
- 33.
- 34.call.setMethodName("HelloWorld");
- 35.
- 36.call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
- 37.
- 38.
- 39.Vector soapParams = new Vector();
- 40.
- 41.soapParams.addElement(new Parameter("temp", String.class, u, null));
- 42.
- 43.call.setParams(soapParams);
- 44.
- 45.
- 46.Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld");
- 47.
- 48.
- 49.if (soapResponse.generatedFault()) {
- 50.
- 51.Fault fault = soapResponse.getFault();
- 52.
- 53.System.out.println(fault);
- 54.
- 55.} else {
- 56.
- 57.Parameter soapResult = soapResponse.getReturnValue();
- 58.
- 59.Object obj = soapResult.getValue();
- 60.
- 61.System.out.println("===" + obj);
- 62.
- 63.}
- 64.
- 65.} catch (Exception e) {
- 66.
- 67.e.printStackTrace();
- 68.
- 69.}
- 70.
- 71.return null;
- 72.}
- 73./**
- 74. * 调用 C# 的webservice接口
- 75. * SoapRpcMethod(Action = "http://www.tangs.com/Add",
- 76. * RequestNamespace = "http://www.tangs.com/T",
- 77. * ResponseNamespace = "http://www.tangs.com/T",
- 78. * Use = SoapBindingUse.Literal)]
- 79. *
- 80. * */
- 81. public static void addTest() {
- 82. try {
- 83. Integer i = 1;
- 84. Integer j = 2;
- 85.
- 86. // WebService URL
- 87. String service_url = "http://localhost:4079/ws/Service.asmx";
- 88.
- 89. Service service = new Service();
- 90. Call call = (Call) service.createCall();
- 91. call.setTargetEndpointAddress(new java.net.URL(service_url));
- 92.
- 93. // 设置要调用的方法
- 94. call.setOperationName(new QName("http://www.tangs.com/T", "Add"));
- 95.
- 96. // 该方法需要的参数
- 97. call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);
- 98. call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);
- 99.
- 100. // 方法的返回值类型
- 101. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);
- 102.
- 103. call.setUseSOAPAction(true);
- 104. call.setSOAPActionURI("http://www.tangs.com/Add");
- 105.
- 106. // 调用该方法
- 107. Integer res = (Integer) call.invoke(new Object[] { i, j });
- 108.
- 109. System.out.println("Result: " + res.toString());
- 110.
- 111. } catch (Exception e) {
- 112. System.err.println(e);
- 113. }
- 114. }
- 115.
- 116. /**
- 117. * 调用 C# 的webservice接口
- 118. * SoapRpcMethod(Action = "http://www.tangs.com/Add",
- 119. * RequestNamespace = "http://www.tangs.com/T",
- 120. * ResponseNamespace = "http://www.tangs.com/T",
- 121. * Use = SoapBindingUse.Literal)]
- 122. *
- 123. * */
- 124. public static void helloTest() {
- 125. try {
- 126.
- 127. String endpoint = "http://localhost:4079/ws/Service.asmx";
- 128. Service service = new Service();
- 129. Call call = (Call) service.createCall();
- 130. call.setTargetEndpointAddress(new java.net.URL(endpoint));
- 131. call.setOperationName(new QName("http://www.tangs.com/T", "HelloWorld"));
- 132.
- 133. call.setUseSOAPAction(true);
- 134. call.setSOAPActionURI("http://www.tangs.com/Hello");
- 135.
- 136. String res = (String) call.invoke(new Object[] { null });
- 137.
- 138. System.out.println("Result: " + res);
- 139. } catch (Exception e) {
- 140. System.err.println(e.toString());
- 141. }
- 142. }
复制代码 |
|