【问题标题】:How to return an XML with node-soap server?如何使用 node-soap 服务器返回 XML?
【发布时间】:2020-07-20 20:18:18
【问题描述】:

我正在使用 Express 制作一个包含 API REST 和 SOAP 方法(我使用 https://www.npmjs.com/package/soap)的 NodeJS Web 服务。使用 API REST 我没有任何问题,但使用 SOAP 我有一个不便,当我尝试从测试 C# 应用程序中使用 SOAP 方法时,我可以看到参数运行良好,但在响应中我有下一个错误在 C# 中(响应不是正确的 XML 代码)

当我使用 node-soap 从 NodeJS 客户端使用该方法时,响应工作正常。

我的部分 NodeJS 代码:

const express = require('express');
const bodyParser = require('body-parser');
const soap = require('soap');
const fs = require('fs');

const xml = fs.readFileSync('src/templates/ws_soap.wsdl', 'utf8');

const app = express();

app.use(bodyParser.urlencoded({
  extended: false
}));

app.use(bodyParser.json());

const soap_service = {
  integrations: {
    pull: {
      getSnapshotGIGA: function(args) {
        return {
          res: "HOLA"
        };
      },
    }
  }
};

app.listen(port, ip, function() {

  soap.listen(app, '/integrations_service', soap_service, xml, function() {
    console.log('SOAP web service started on ' + ip + ':' + port);
  });

  console.log('API REST started on ' + ip + ':' + port);
});

接下来是我的 WSDL 文件(作为响应,我有类型字符串,因为我想看看它的行为方式,但我需要返回一个对象 XML):

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions name="integrations_service" targetNamespace="http://localhost:4205/integrations_service" xmlns="http://localhost:4205/integrations_service" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <wsdl:message name="getSnapshotGIGARequest">
    <wsdl:part name="User" type="xs:string"/>
    <wsdl:part name="Password" type="xs:string"/>
  </wsdl:message>
  <wsdl:message name="getSnapshotGIGAResponse">
    <wsdl:part name="res" type="xs:string"/>
  </wsdl:message>
  <wsdl:portType name="pull_integrations">
    <wsdl:operation name="getSnapshotGIGA">
      <wsdl:input message="getSnapshotGIGARequest"/>
      <wsdl:output message="getSnapshotGIGAResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="pull_integrations_binding" type="pull_integrations">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="getSnapshotGIGA">
      <soap:operation soapAction="getSnapshotGIGA"/>
      <wsdl:input>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="integrations">
    <wsdl:port binding="pull_integrations_binding" name="pull">
      <soap:address location="http://localhost:4205/integrations_service"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

在 C# 中,我有一个控制台应用程序,并且我已将 SOAP 服务注册为 Web 引用。

我使用 SOAP 方法的方式是(当我使用 C# 制作 SOAP 服务时,我也会通过这种方式测试方法,因为这是客户端工作的方式):

Console.WriteLine("Consume NodeJS SOAP service");
Thread.Sleep(500);
integrations_service.integrations integrations = new integrations_service.integrations();
integrations.Url = "http://localhost:4205/integrations_service?wsdl";
var some_response = integrations.getSnapshotGIGA("myuser", "123456");
Console.WriteLine("Press enter to out...");

我想像这个例子一样在 XmlNode 中获得响应:

Console.WriteLine("Consume C# SOAP service");
Thread.Sleep(500);
serviceSOAP sSOAP = new serviceSOAP ();
sSOAP.Url = "http://my.domain.com.mx/";
XmlNode xmlNode = sSOAP .anyMethodSoap("yomero", "123456");
Console.WriteLine(XElement.Parse(xmlNode.OuterXml).ToString());
Thread.Sleep(500);

如果您知道如何从 NodeJS 返回 XML 并在 C# 或任何想法中正确获取它,我将不胜感激。问候。

【问题讨论】:

    标签: c# node.js soap soap-client node-soap


    【解决方案1】:

    回答我的问题,问题在于 WSDL 配置。使用此配置,我将其与 C# Web 参考一起使用。主要问题是样式,我将其从“rpc”更改为“document”,并正确配置了响应元素。

    <?xml version="1.0" encoding="UTF-8"?>
    <wsdl:definitions
      name="devices_service"
      targetNamespace="http://localhost:4205/devices_service"
      xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
      xmlns:s="http://schemas.xmlsoap.org/wsdl/soap/"
      xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns="http://localhost:4205/devices_service">
      <wsdl:types>
        <xs:schema targetNamespace="http://localhost:4205/devices_service" xmlns="http://localhost:4205/devices_service" attributeFormDefault="qualified" elementFormDefault="qualified">
          <xs:element name="GetDevicesRequest">
            <xs:complexType>
              <xs:sequence>
                <xs:element minOccurs="0" maxOccurs="1" name="User" type="xs:string"/>
                <xs:element minOccurs="0" maxOccurs="1" name="Password" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
          <xs:element name="GetDevicesResponse">
            <xs:complexType>
              <xs:sequence>
                <xs:any/>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:schema>
      </wsdl:types>
      <wsdl:message name="GetDevicesSoapIn">
        <wsdl:part name="parameters" element="GetDevicesRequest"/>
      </wsdl:message>
      <wsdl:message name="GetDevicesSoapOut">
        <wsdl:part name="parameters" element="GetDevicesResponse"/>
      </wsdl:message>
      <wsdl:portType name="user_devices">
        <wsdl:operation name="GetDevices">
          <wsdl:input message="GetDevicesSoapIn"/>
          <wsdl:output message="GetDevicesSoapOut"/>
        </wsdl:operation>
      </wsdl:portType>
      <wsdl:binding name="user_devices_binding" type="user_devices">
        <s:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
        <wsdl:operation name="GetDevices">
          <s:operation soapAction="http://localhost:4205/devices_services/GetDevices"/>
          <wsdl:input>
            <s:body use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
          </wsdl:input>
          <wsdl:output>
            <s:body use="literal" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
          </wsdl:output>
        </wsdl:operation>
      </wsdl:binding>
      <wsdl:service name="devices">
        <wsdl:port binding="user_devices_binding" name="user">
          <s:address location="http://localhost:4205/devices_service"/>
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>**
    

    【讨论】:

      【解决方案2】:

      您可能想要使用以下之一:

      节点肥皂 strong-soap(重写 node-soap) 易肥皂

      【讨论】:

        猜你喜欢
        • 2014-01-13
        • 2016-05-21
        • 1970-01-01
        • 2018-10-02
        • 1970-01-01
        • 2012-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多