开发动态Web服务调用框架(3)

本文介绍了使用Java通过Axis和SOAP两种方式调用WebService的方法。包括设置目标地址、操作名称及参数类型等步骤,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在优快云上,看到了一些直接能够调用WEB服务的代码,觉的后期可以用这种技术,通过解析WSDL,得到相应信息,然后动态调用。由于最近期末考试,就先不写了!

  1. //Java - Webservice调用方式详解
  2. //调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同
  3. //方式:axis和Soap,soap方式主要是用在websphere下
  4. //axis方式调用:
  5. import java.util.Date;
  6. import java.text.DateFormat;
  7. import org.apache.axis.client.Call;
  8. import org.apache.axis.client.Service;
  9. import javax.xml.namespace.QName;
  10. import java.lang.Integer;
  11. import javax.xml.rpc.ParameterMode;
  12. public class caClient {
  13. public static void main(String[] args) {
  14. try {
  15. String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";
  16. Service service = new Service();
  17. Call call = (Call) service.createCall();
  18. call.setTargetEndpointAddress(endpoint);
  19. call.setOperationName("addUser");
  20. call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
  21. javax.xml.rpc.ParameterMode.IN);
  22. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
  23. call.setUseSOAPAction(true);
  24. call.setSOAPActionURI("http://www.my.com/Rpc");
  25. //Integer k = (Integer) call.invoke(new Object[] { i, j });
  26. //System.out.println("result is " + k.toString() + ".");
  27. String temp = "测试人员";
  28. String result = (String)call.invoke(new Object[]{temp});
  29. System.out.println("result is "+result);
  30. }
  31. catch (Exception e) {
  32. System.err.println(e.toString());
  33. }
  34. }
  35. }
  36. //soap方式调用
  37. import org.apache.soap.util.xml.*;
  38. import org.apache.soap.*;
  39. import org.apache.soap.rpc.*;
  40. import java.io.*;
  41. import java.net.*;
  42. import java.util.Vector;
  43. public class caService{
  44. public static String getService(String user) {
  45. URL url = null;
  46. try {
  47. url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");
  48. catch (MalformedURLException mue) {
  49. return mue.getMessage();
  50. }
  51. // This is the main SOAP object
  52. Call soapCall = new Call();
  53. // Use SOAP encoding
  54. soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
  55. // This is the remote object we're asking for the price
  56. soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
  57. // This is the name of the method on the above object
  58. soapCall.setMethodName("getUser");
  59. // We need to send the ISBN number as an input parameter to the method
  60. Vector soapParams = new Vector();
  61. // name, type, value, encoding style
  62. Parameter isbnParam = new Parameter("userName", String.class, user, null);
  63. soapParams.addElement(isbnParam);
  64. soapCall.setParams(soapParams);
  65. try {
  66. // Invoke the remote method on the object
  67. Response soapResponse = soapCall.invoke(url,"");
  68. // Check to see if there is an error, return "N/A"
  69. if (soapResponse.generatedFault()) {
  70. Fault fault = soapResponse.getFault();
  71. String f = fault.getFaultString();
  72. return f;
  73. else {
  74. // read result
  75. Parameter soapResult = soapResponse.getReturnValue ();
  76. // get a string from the result
  77. return soapResult.getValue().toString();
  78. }
  79. catch (SOAPException se) {
  80. return se.getMessage();
  81. }
  82. }
  83. }
  84. //返回一维数组时
  85. Parameter soapResult = soapResponse.getReturnValue();
  86. String[] temp = (String[])soapResult.getValue();
  87. //调用ASP.Net生成的webservice
  88. private String HelloWorld(String uri, String u) {
  89. try {
  90. SOAPMappingRegistry smr = new SOAPMappingRegistry();
  91. StringDeserializer sd = new StringDeserializer();
  92. ArraySerializer arraySer = new ArraySerializer();
  93. BeanSerializer beanSer = new BeanSerializer();
  94. smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(
  95. "http://tempuri.org/""HelloWorldResult"), String.class,
  96. null, sd);
  97. smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(
  98. "http://tempuri.org/""temp"), String.class,
  99. beanSer, beanSer);
  100. URL url = new URL(uri);
  101. Call call = new Call();
  102. call.setSOAPMappingRegistry(smr);
  103. call.setTargetObjectURI("urn:xmethods-Service1");
  104. call.setMethodName("HelloWorld");
  105. call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
  106. Vector soapParams = new Vector();
  107. soapParams.addElement(new Parameter("temp", String.class, u, null));
  108. call.setParams(soapParams);
  109. Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld");
  110. if (soapResponse.generatedFault()) {
  111. Fault fault = soapResponse.getFault();
  112. System.out.println(fault);
  113. else {
  114. Parameter soapResult = soapResponse.getReturnValue();
  115. Object obj = soapResult.getValue();
  116. System.out.println("===" + obj);
  117. }
  118. catch (Exception e) {
  119. e.printStackTrace();
  120. }
  121. return null;
  122. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值