免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3193 | 回复: 0
打印 上一主题 下一主题

Webservice调用方式:axis,soap详解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-29 14:40 |只看该作者 |倒序浏览
Webservice调用方式:axis,soap详解






转自: [url]http://blog.csdn.net/baiboy4493/archive/2009/03/13/3987526.aspx [/url]
调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis和Soap,soap方式主要是用在websphere下

axis方式调用:


Java代码
  1. 1.import java.util.Date;   
  2. 2.  
  3. 3.import java.text.DateFormat;   
  4. 4.  
  5. 5.import org.apache.axis.client.Call;   
  6. 6.  
  7. 7.import org.apache.axis.client.Service;   
  8. 8.  
  9. 9.import javax.xml.namespace.QName;   
  10. 10.  
  11. 11.import java.lang.Integer;   
  12. 12.  
  13. 13.import javax.xml.rpc.ParameterMode;   
  14. 14.  
  15. 15.  
  16. 16.public class caClient {   
  17. 17.  
  18. 18.  
  19. 19.  
  20. 20.public static void main(String[] args) {   
  21. 21.  
  22. 22.  
  23. 23.try {   
  24. 24.  
  25. 25.String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";   
  26. 26.  
  27. 27.Service service = new Service();   
  28. 28.  
  29. 29.Call call = (Call) service.createCall();   
  30. 30.  
  31. 31.call.setTargetEndpointAddress(endpoint);   
  32. 32.  
  33. 33.call.setOperationName("addUser");   
  34. 34.  
  35. 35.call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,   
  36. 36.  
  37. 37.javax.xml.rpc.ParameterMode.IN);   
  38. 38.  
  39. 39.call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);   
  40. 40.  
  41. 41.call.setUseSOAPAction(true);   
  42. 42.  
  43. 43.call.setSOAPActionURI("http://www.my.com/Rpc");   
  44. 44.  
  45. 45.//Integer k = (Integer) call.invoke(new Object[] { i, j });   
  46. 46.  
  47. 47.//System.out.println("result is " + k.toString() + ".");   
  48. 48.  
  49. 49.String temp = "测试人员";   
  50. 50.  
  51. 51.String result = (String)call.invoke(new Object[]{temp});   
  52. 52.  
  53. 53.System.out.println("result is "+result);   
  54. 54.  
  55. 55.}   
  56. 56.  
  57. 57.catch (Exception e) {   
  58. 58.  
  59. 59.System.err.println(e.toString());   
  60. 60.  
  61. 61.}   
  62. 62.  
  63. 63.}   
  64. 64.  
  65. 65.}  
  66. import java.util.Date;

  67. import java.text.DateFormat;

  68. import org.apache.axis.client.Call;

  69. import org.apache.axis.client.Service;

  70. import javax.xml.namespace.QName;

  71. import java.lang.Integer;

  72. import javax.xml.rpc.ParameterMode;


  73. public class caClient {



  74. public static void main(String[] args) {


  75. try {

  76. String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";

  77. Service service = new Service();

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

  79. call.setTargetEndpointAddress(endpoint);

  80. call.setOperationName("addUser");

  81. call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,

  82. javax.xml.rpc.ParameterMode.IN);

  83. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);

  84. call.setUseSOAPAction(true);

  85. call.setSOAPActionURI("http://www.my.com/Rpc");

  86. //Integer k = (Integer) call.invoke(new Object[] { i, j });

  87. //System.out.println("result is " + k.toString() + ".");

  88. String temp = "测试人员";

  89. String result = (String)call.invoke(new Object[]{temp});

  90. System.out.println("result is "+result);

  91. }

  92. catch (Exception e) {

  93. System.err.println(e.toString());

  94. }

  95. }

  96. }
复制代码
soap方式调用

调用java生成的webservice



Java代码
  1. 1.import org.apache.soap.util.xml.*;   
  2. 2.  
  3. 3.import org.apache.soap.*;   
  4. 4.  
  5. 5.import org.apache.soap.rpc.*;   
  6. 6.  
  7. 7.  
  8. 8.import java.io.*;   
  9. 9.  
  10. 10.import java.net.*;   
  11. 11.  
  12. 12.import java.util.Vector;   
  13. 13.  
  14. 14.  
  15. 15.public class caService{   
  16. 16.  
  17. 17.public static String getService(String user) {   
  18. 18.  
  19. 19.URL url = null;   
  20. 20.  
  21. 21.try {   
  22. 22.  
  23. 23.url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");   
  24. 24.  
  25. 25.} catch (MalformedURLException mue) {   
  26. 26.  
  27. 27.return mue.getMessage();   
  28. 28.  
  29. 29.}   
  30. 30.  
  31. 31.// This is the main SOAP object   
  32. 32.  
  33. 33.Call soapCall = new Call();   
  34. 34.  
  35. 35.// Use SOAP encoding   
  36. 36.  
  37. 37.soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);   
  38. 38.  
  39. 39.// This is the remote object we're asking for the price   
  40. 40.  
  41. 41.soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");   
  42. 42.  
  43. 43.// This is the name of the method on the above object   
  44. 44.  
  45. 45.soapCall.setMethodName("getUser");   
  46. 46.  
  47. 47.// We need to send the ISBN number as an input parameter to the method   
  48. 48.  
  49. 49.Vector soapParams = new Vector();   
  50. 50.  
  51. 51.  
  52. 52.// name, type, value, encoding style   
  53. 53.  
  54. 54.Parameter isbnParam = new Parameter("userName", String.class, user, null);   
  55. 55.  
  56. 56.soapParams.addElement(isbnParam);   
  57. 57.  
  58. 58.soapCall.setParams(soapParams);   
  59. 59.  
  60. 60.try {   
  61. 61.  
  62. 62.// Invoke the remote method on the object   
  63. 63.  
  64. 64.Response soapResponse = soapCall.invoke(url,"");   
  65. 65.  
  66. 66.// Check to see if there is an error, return "N/A"   
  67. 67.  
  68. 68.if (soapResponse.generatedFault()) {   
  69. 69.  
  70. 70.Fault fault = soapResponse.getFault();   
  71. 71.  
  72. 72.String f = fault.getFaultString();   
  73. 73.  
  74. 74.return f;   
  75. 75.  
  76. 76.} else {   
  77. 77.  
  78. 78.// read result   
  79. 79.  
  80. 80.Parameter soapResult = soapResponse.getReturnValue ();   
  81. 81.  
  82. 82.// get a string from the result   
  83. 83.  
  84. 84.return soapResult.getValue().toString();   
  85. 85.  
  86. 86.}   
  87. 87.  
  88. 88.} catch (SOAPException se) {   
  89. 89.  
  90. 90.return se.getMessage();   
  91. 91.  
  92. 92.}   
  93. 93.  
  94. 94.}   
  95. 95.  
  96. 96.}  
  97. import org.apache.soap.util.xml.*;

  98. import org.apache.soap.*;

  99. import org.apache.soap.rpc.*;


  100. import java.io.*;

  101. import java.net.*;

  102. import java.util.Vector;


  103. public class caService{

  104. public static String getService(String user) {

  105. URL url = null;

  106. try {

  107. url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");

  108. } catch (MalformedURLException mue) {

  109. return mue.getMessage();

  110. }

  111. // This is the main SOAP object

  112. Call soapCall = new Call();

  113. // Use SOAP encoding

  114. soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

  115. // This is the remote object we're asking for the price

  116. soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");

  117. // This is the name of the method on the above object

  118. soapCall.setMethodName("getUser");

  119. // We need to send the ISBN number as an input parameter to the method

  120. Vector soapParams = new Vector();


  121. // name, type, value, encoding style

  122. Parameter isbnParam = new Parameter("userName", String.class, user, null);

  123. soapParams.addElement(isbnParam);

  124. soapCall.setParams(soapParams);

  125. try {

  126. // Invoke the remote method on the object

  127. Response soapResponse = soapCall.invoke(url,"");

  128. // Check to see if there is an error, return "N/A"

  129. if (soapResponse.generatedFault()) {

  130. Fault fault = soapResponse.getFault();

  131. String f = fault.getFaultString();

  132. return f;

  133. } else {

  134. // read result

  135. Parameter soapResult = soapResponse.getReturnValue ();

  136. // get a string from the result

  137. return soapResult.getValue().toString();

  138. }

  139. } catch (SOAPException se) {

  140. return se.getMessage();

  141. }

  142. }

  143. }
复制代码
返回一维数组时


Java代码
  1. 1.Parameter soapResult = soapResponse.getReturnValue();   
  2. 2.  
  3. 3.String[] temp = (String[])soapResult.getValue();  
  4. Parameter soapResult = soapResponse.getReturnValue();

  5. String[] temp = (String[])soapResult.getValue();
复制代码
调用ASP.Net生成的webservice


Java代码
  1. 1.private String HelloWorld(String uri, String u) {   
  2. 2.  
  3. 3.try {   
  4. 4.  
  5. 5.SOAPMappingRegistry smr = new SOAPMappingRegistry();   
  6. 6.  
  7. 7.StringDeserializer sd = new StringDeserializer();   
  8. 8.  
  9. 9.ArraySerializer arraySer = new ArraySerializer();   
  10. 10.  
  11. 11.BeanSerializer beanSer = new BeanSerializer();   
  12. 12.  
  13. 13.smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(   
  14. 14.  
  15. 15."http://tempuri.org/", "HelloWorldResult"), String.class,   
  16. 16.  
  17. 17.null, sd);   
  18. 18.  
  19. 19.smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(   
  20. 20.  
  21. 21."http://tempuri.org/", "temp"), String.class,   
  22. 22.  
  23. 23.beanSer, beanSer);   
  24. 24.  
  25. 25.  
  26. 26.URL url = new URL(uri);   
  27. 27.  
  28. 28.Call call = new Call();   
  29. 29.  
  30. 30.call.setSOAPMappingRegistry(smr);   
  31. 31.  
  32. 32.call.setTargetObjectURI("urn:xmethods-Service1");   
  33. 33.  
  34. 34.call.setMethodName("HelloWorld");   
  35. 35.  
  36. 36.call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);   
  37. 37.  
  38. 38.  
  39. 39.Vector soapParams = new Vector();   
  40. 40.  
  41. 41.soapParams.addElement(new Parameter("temp", String.class, u, null));   
  42. 42.  
  43. 43.call.setParams(soapParams);   
  44. 44.  
  45. 45.  
  46. 46.Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld");   
  47. 47.  
  48. 48.  
  49. 49.if (soapResponse.generatedFault()) {   
  50. 50.  
  51. 51.Fault fault = soapResponse.getFault();   
  52. 52.  
  53. 53.System.out.println(fault);   
  54. 54.  
  55. 55.} else {   
  56. 56.  
  57. 57.Parameter soapResult = soapResponse.getReturnValue();   
  58. 58.  
  59. 59.Object obj = soapResult.getValue();   
  60. 60.  
  61. 61.System.out.println("===" + obj);   
  62. 62.  
  63. 63.}   
  64. 64.  
  65. 65.} catch (Exception e) {   
  66. 66.  
  67. 67.e.printStackTrace();   
  68. 68.  
  69. 69.}   
  70. 70.  
  71. 71.return null;   
  72. 72.}   
  73. 73./**  
  74. 74.  * 调用 C# 的webservice接口  
  75. 75.  * SoapRpcMethod(Action = "http://www.tangs.com/Add",   
  76. 76.  * RequestNamespace = "http://www.tangs.com/T",   
  77. 77.  * ResponseNamespace = "http://www.tangs.com/T",   
  78. 78.  * Use = SoapBindingUse.Literal)]  
  79. 79.  *   
  80. 80.  * */  
  81. 81. public static void addTest() {   
  82. 82.  try {   
  83. 83.   Integer i = 1;   
  84. 84.   Integer j = 2;   
  85. 85.  
  86. 86.   // WebService URL   
  87. 87.   String service_url = "http://localhost:4079/ws/Service.asmx";   
  88. 88.  
  89. 89.   Service service = new Service();   
  90. 90.   Call call = (Call) service.createCall();   
  91. 91.   call.setTargetEndpointAddress(new java.net.URL(service_url));   
  92. 92.  
  93. 93.   // 设置要调用的方法   
  94. 94.   call.setOperationName(new QName("http://www.tangs.com/T", "Add"));   
  95. 95.  
  96. 96.   // 该方法需要的参数   
  97. 97.   call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);   
  98. 98.   call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);   
  99. 99.  
  100. 100.   // 方法的返回值类型   
  101. 101.   call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);   
  102. 102.  
  103. 103.   call.setUseSOAPAction(true);   
  104. 104.   call.setSOAPActionURI("http://www.tangs.com/Add");   
  105. 105.  
  106. 106.   // 调用该方法   
  107. 107.   Integer res = (Integer) call.invoke(new Object[] { i, j });   
  108. 108.  
  109. 109.   System.out.println("Result: " + res.toString());   
  110. 110.  
  111. 111.  } catch (Exception e) {   
  112. 112.   System.err.println(e);   
  113. 113.  }   
  114. 114. }   
  115. 115.  
  116. 116. /**  
  117. 117.  * 调用 C# 的webservice接口  
  118. 118.  * SoapRpcMethod(Action = "http://www.tangs.com/Add",   
  119. 119.  * RequestNamespace = "http://www.tangs.com/T",   
  120. 120.  * ResponseNamespace = "http://www.tangs.com/T",   
  121. 121.  * Use = SoapBindingUse.Literal)]  
  122. 122.  *   
  123. 123.  * */  
  124. 124. public static void helloTest() {   
  125. 125.  try {   
  126. 126.  
  127. 127.   String endpoint = "http://localhost:4079/ws/Service.asmx";   
  128. 128.   Service service = new Service();   
  129. 129.   Call call = (Call) service.createCall();   
  130. 130.   call.setTargetEndpointAddress(new java.net.URL(endpoint));   
  131. 131.   call.setOperationName(new QName("http://www.tangs.com/T", "HelloWorld"));   
  132. 132.  
  133. 133.   call.setUseSOAPAction(true);   
  134. 134.   call.setSOAPActionURI("http://www.tangs.com/Hello");   
  135. 135.  
  136. 136.   String res = (String) call.invoke(new Object[] { null });   
  137. 137.  
  138. 138.   System.out.println("Result: " + res);   
  139. 139.  } catch (Exception e) {   
  140. 140.   System.err.println(e.toString());   
  141. 141.  }   
  142. 142. }  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP