加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

转 ofbiz的webservice接口提供(3)-不规范的wsdl的客户端访问代

发布时间:2020-12-16 23:24:07 所属栏目:安全 来源:网络整理
导读:针对上个模块提到的ofbiz的wsdl确实不是很规范,那么我们使用axis客户端工具生成的代码肯定不可用,这里我提供了我的客户端调用代码: [java] view plain copy import java.util.*;? import java.net.*;? import java.rmi.*;? import javax.xml.namespace.*;

针对上个模块提到的ofbiz的wsdl确实不是很规范,那么我们使用axis客户端工具生成的代码肯定不可用,这里我提供了我的客户端调用代码:

[java] view plain copy
  1. import java.util.*;?
  2. import java.net.*;?
  3. import java.rmi.*;?
  4. import javax.xml.namespace.*;?
  5. import javax.xml.rpc.*;?
  6. import org.apache.axis.Message;?
  7. import org.apache.axis.message.RPCElement;?
  8. import org.apache.axis.message.RPCParam;?
  9. import org.apache.axis.message.SOAPEnvelope;?
  10. import org.apache.axis.client.Call;?
  11. import org.apache.axis.client.Service;?
  12. ?
  13. public class AxisClient {?????
  14. ???? /**
  15. ????? * 将我们的消息在控制台System.out打印出来
  16. ????? * */?
  17. ???? private static Map getResponseParams(Message respMessage) {?
  18. ??????????? Map mRet = new Hashtable();?
  19. ??????????? try {?
  20. SOAPEnvelope resEnv = respMessage.getSOAPEnvelope();
  21. ??????????????? List bodies = resEnv.getBodyElements();?
  22. ??????????????? Iterator i = bodies.iterator();?
  23. ??????????????? while (i.hasNext()) {?
  24. ??????????????????? Object o = i.next();?
  25. ??????????????????? if (o instanceof RPCElement) {?
  26. ??????????????????????? RPCElement body = (RPCElement) o;?
  27. ??????????????????????? List params = null;?
  28. ??????????????????????? params = body.getParams();?
  29. ??????????????????????? Iterator p = params.iterator();?
  30. ??????????????????????? while (p.hasNext()) {?
  31. ??????????????????????????? RPCParam param = (RPCParam) p.next();?
  32. ??????????????????????????? mRet.put(param.getName(),param.getValue());?
  33. ??????????????????????????? System.out.println("SOAP Client Param - " + param.getName() + "=" + param.getValue());?
  34. ??????????????????????? }?
  35. ??????????????????? }?
  36. ??????????????? }?
  37. ??????????? } catch (org.apache.axis.AxisFault e) {?
  38. ???????????? System.out.println("AxisFault");?
  39. ??????????? } catch (org.xml.sax.SAXException e) {?
  40. ???????????? System.out.println("SAXException");?
  41. ??????????? }?
  42. ??????????? return mRet;?
  43. ??????? }?
  44. ????????
  45. ??????? public static void main(String[] args) {?
  46. ??????? String message = "";?
  47. ??????? Map output;?
  48. ??????? String endpoint;?
  49. ??????? try {?
  50. ??????????? //指明我们的服务点?
  51. ??????????? endpoint = "http://192.168.20.32/projectname/control/SOAPService/";?
  52. ??????????? Call call = (Call) new Service().createCall();?
  53. ??????????? call.setTargetEndpointAddress(new URL(endpoint));?
  54. ??????????? //指明要调用的服务名称?
  55. ??????????? call.setOperationName(new QName("findSeniorService","findSeniorService"));?
  56. ??????????? //指明服务的输出输出参数?
  57. ??????????? call.addParameter("userid",?
  58. ????????????????????????????? org.apache.axis.Constants.XSD_STRING,?
  59. ????????????????????????????? javax.xml.rpc.ParameterMode.INOUT);?
  60. ??????????? call.addParameter("salt",?
  61. ??????????????????? org.apache.axis.Constants.XSD_STRING,?
  62. ??????????????????? javax.xml.rpc.ParameterMode.IN);?
  63. ??????????? call.addParameter("aaa",108); border-left-width:3px; list-style-type:decimal-leading-zero; list-style-position:outside!important"> ??????????????????? org.apache.axis.Constants.XSD_STRING,248)"> ??????????????????? javax.xml.rpc.ParameterMode.OUT);?
  64. ??????????? call.addParameter("bbb",108); border-left-width:3px; list-style-type:decimal-leading-zero; list-style-position:outside!important"> ??????????????????? javax.xml.rpc.ParameterMode.OUT);?
  65. ??????????? call.addParameter("ccc",108); border-left-width:3px; list-style-type:decimal-leading-zero; list-style-position:outside!important"> ??????????? call.setReturnType(org.apache.axis.Constants.XSD_STRING);?
  66. ??????????? //传递参数,发起调用?
  67. ??????????? Object responseWS = call.invoke(new Object[]{"123456789","aaa"});?
  68. ??????????? System.out.println( "Receiving response: " +? (String) responseWS);?
  69. ??????????? output = call.getOutputParams();?
  70. ??????????? getResponseParams(call.getMessageContext().getResponseMessage());?
  71. ??????? } catch (MalformedURLException ex) {?
  72. ??????????? message = "error: wrong url";?
  73. ??????? } catch (ServiceException ex) {?
  74. ??????????? message = "error: failed to create the call";?
  75. ??????? } catch (RemoteException ex) {?
  76. ??????????? ex.printStackTrace();?
  77. ??????????? message = "error: failed to invoke WS";?
  78. ??????? } finally {?
  79. ????????? System.out.println("");?
  80. ????????? System.out.println(message);?
  81. ????? }?
  82. }?

注意上边的endpoint的链接要根据你服务器部署的实际情况来书写。

同时我也提供下xmlspy根据链接生成的数据包,这个不可用:

[c-sharp] copy
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">?
  1. ??? <SOAP-ENV:Body>?
  2. ??????? <salt xsi:type="xsd:string">String</salt>?
  3. ??????? <userid xsi:type="xsd:string">String</userid>?
  4. ??? </SOAP-ENV:Body>?
  5. </SOAP-ENV:Envelope>?

看到上边那个生成的不可用的文件主要是没指定服务方法,我们手工改一下,并将我们的参数值奉上:

[xhtml] copy
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">?
  1. ??? <SOAP-ENV:Body>?
  2. ????? <findSeniorService xmlns="http://ofbiz.apache.org/service/">?
  3. ??????? <salt xsi:type="xsd:string">aaa</salt>?
  4. ??????? <userid xsi:type="xsd:string">2222</userid>?
  5. ????? </findSeniorService>?
  6. ??? </SOAP-ENV:Body>?
  7. </SOAP-ENV:Envelope>?

看上边只是指定了我们要给哪个方法传送参数“<findSeniorService xmlns="http://ofbiz.apache.org/service/">”

然后发送soap的信息到webservice接口,我这里的返回值如下:

copy
    <?xml version="1.0" encoding="UTF-8"?>?
  1. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">?
  2. ??? <soapenv:Body>?
  3. ??????? <ns1:findSeniorServiceResponse xmlns:ns1="http://ofbiz.apache.org/service/">?
  4. ??????????? <aaa xsi:type="xsd:string">test_aaaaa</aaa>?
  5. ??????????? <bbb xsi:type="xsd:string">test_bbbbb</bbb>?
  6. ??????????? <ccc xsi:type="xsd:string">test_ccccc</ccc>?
  7. ??????????? <userid xsi:type="xsd:string">2222</userid>?
  8. ??????? </ns1:findSeniorServiceResponse>?
  9. ??? </soapenv:Body>?
  10. </soapenv:Envelope>?

这样我就验证了虽然ofbiz提供的webservice的wsdl很不好用,但是那个webservice接口还是可以使用的。只不过只是支持基础数据类型而已。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读