外部系统调用Ofbiz中的服务方法

Ofbiz默认支持提供Soap的WebService服务。需要在Service的定义文件中,增加属性export=true.

本文将以getAllCategories服务为例,该服务是在Product模块下的servcie-maint.xml中。

首先增加export="true",如下:

    <service name="getAllCategories" engine="simple" export="true"
      location="component://product/minilang/product/catalog/CatalogServices.xml" invoke="getAllCategories">
        <description>Get all categories of a category </description>
        <attribute name="topCategory" type="String" mode="IN" optional="true"/>
        <attribute name="categories" type="java.util.List" mode="OUT" optional="true"/>
    </service>

然后重启ofbiz服务,此时就可以查看该服务的WSDL了,路径为:http://192.168.122.9:8080/webtools/control/SOAPService/getAllCategories?wsdl.可以通过第三方工具SoapUI进行测试。测试如下:(若需要在Ajax中调用,可直接使用http://192.168.122.9:8080/webtools/control/httpService?serviceName=getAllCategories&serviceContext=***,serviceContext为参数的Map格式序列化)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://ofbiz.apache.org/service/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getAllCategories>
         <map-Map>
            <ser:map-Entry>
               <ser:map-Key>
                  <ser:std-String value="login.username"/>
               </ser:map-Key>
               <ser:map-Value>
                  <ser:std-String value="admin"/>
               </ser:map-Value>
               <ser:map-Key>
                  <ser:std-String value="login.password"/>
               </ser:map-Key>
               <ser:map-Value>
                  <ser:std-String value="ofbiz"/>
               </ser:map-Value>
               <ser:map-Key>
                  <ser:std-String value="topCategory"/>
               </ser:map-Key>
               <ser:map-Value>
                  <ser:std-String value=""/>
               </ser:map-Value>
            </ser:map-Entry>
         </map-Map>
      </ser:getAllCategories>
   </soapenv:Body>
</soapenv:Envelope>

可以看到,代码中除了服务所必须的参数topCategory 外,还额外增加了用户的登录账号和密码。参数是以Map的Key-Value对的形式传入的。传出的结果也是如此。

经我测试,C#代码直接引入WebService调用未能成功。此处采用HttpWebRequest类Post发送Xml数据的形式实现:

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://192.168.122.9:8080/webtools/control/SOAPService");
            webRequest.Headers.Add("SOAPAction", "https://192.168.122.9:8443/webtools/control/SOAPService");
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";

            XmlDocument soapEnvelopeDocument = new XmlDocument();
            soapEnvelopeDocument.LoadXml(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ser=""http://ofbiz.apache.org/service/""><soapenv:Header/><soapenv:Body><ser:getAllCategories><map-Map><ser:map-Entry><ser:map-Key><ser:std-String value=""login.username""/></ser:map-Key><ser:map-Value><ser:std-String value=""admin""/></ser:map-Value><ser:map-Key><ser:std-String value=""login.password""/></ser:map-Key><ser:map-Value><ser:std-String value=""ofbiz""/></ser:map-Value><ser:map-Key><ser:std-String value=""topCategory""/></ser:map-Key><ser:map-Value><ser:std-String value=""""/></ser:map-Value></ser:map-Entry></map-Map></ser:getAllCategories></soapenv:Body></soapenv:Envelope>");

            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeDocument.Save(stream);
            }

            using (var webResponse = webRequest.GetResponse())
            {
                using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                {
                    var soapResult = rd.ReadToEnd();
                }
            }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值