Node.js:如何使用SOAP XML Web服务

我想知道使用node.js使用SOAP XML Web服务的最佳方法是什么

谢谢!

WHITECOLOR asked 2019-09-09T02:26:16Z
11个解决方案
73 votes

你没有那么多选择。

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

  • 节点皂
  • 强肥皂(重写node-soap
  • easysoap
Juicy Scripter answered 2019-09-09T02:29:37Z
30 votes

我认为另一种选择是:

  • 使用SoapUI([http://www.soapui.org)]等工具来记录输入和输出xml消息
  • 使用节点请求([https://github.com/mikeal/request)]形成输入xml消息,将请求发送(POST)到Web服务(注意标准的javascript模板机制,如ejs([http:// embeddedjs.com/)]或小胡子([https://github.com/janl/mustache.js]]可以帮到你)最后
  • 使用XML解析器将响应数据反序列化为JavaScript对象

是的,这是一个相当肮脏和低级别的方法,但它应该没有问题

tmanolatos answered 2019-09-09T02:30:36Z
14 votes

我发现使用Node.js将原始XML发送到SOAP服务的最简单方法是使用Node.js http实现。 看起来像这样。

var http = require('http');
var http_options = {
  hostname: 'localhost',
  port: 80,
  path: '/LocationOfSOAPServer/',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': xml.length
  }
}

var req = http.request(http_options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });

  res.on('end', () => {
    console.log('No more data in response.')
  })
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(xml); // xml would have been set somewhere to a complete xml document in the form of a string
req.end();

您可以将xml变量定义为字符串形式的原始xml。

但是,如果您只想通过Node.js与SOAP服务进行交互并进行常规SOAP调用,而不是发送原始xml,请使用其中一个Node.js库。 我喜欢node-soap。

Halfstop answered 2019-09-09T02:31:13Z
14 votes

如果Raw不适合您,只需使用HTML request模块,然后根据需要将xml转换为json。

我的请求不适用于Raw并且除了付费支持之外没有对该模块的支持,这超出了我的资源。 所以我做了以下事情:

  1. 在我的Linux机器上下载了SoapUI。
  2. 将WSDL xml复制到本地文件
    Raw
  3. 在SoapUI我去了Raw并上传了我的HTML
  4. 在导航器中,我展开了其中一项服务并右键单击请求并点击Raw

从那里我可以发送请求并确保它有效,我也可以使用RawHTML数据来帮助我构建外部请求。

来自SoapUI的Raw我的请求

POST http://192.168.0.28:10005/MainService/WindowsService HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "http://Main.Service/AUserService/GetUsers"
Content-Length: 303
Host: 192.168.0.28:10005
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

来自SoapUI的XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:qtre="http://Main.Service">
   <soapenv:Header/>
   <soapenv:Body>
      <qtre:GetUsers>
         <qtre:sSearchText></qtre:sSearchText>
      </qtre:GetUsers>
   </soapenv:Body>
</soapenv:Envelope> 

我用以上来构建以下node request

var request = require('request');
let xml =
`<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:qtre="http://Main.Service">
   <soapenv:Header/>
   <soapenv:Body>
      <qtre:GetUsers>
         <qtre:sSearchText></qtre:sSearchText>
      </qtre:GetUsers>
   </soapenv:Body>
</soapenv:Envelope>`

var options = {
  url: 'http://192.168.0.28:10005/MainService/WindowsService?wsdl',
  method: 'POST',
  body: xml,
  headers: {
    'Content-Type':'text/xml;charset=utf-8',
    'Accept-Encoding': 'gzip,deflate',
    'Content-Length':xml.length,
    'SOAPAction':"http://Main.Service/AUserService/GetUsers"
  }
};

let callback = (error, response, body) => {
  if (!error && response.statusCode == 200) {
    console.log('Raw result', body);
    var xml2js = require('xml2js');
    var parser = new xml2js.Parser({explicitArray: false, trim: true});
    parser.parseString(body, (err, result) => {
      console.log('JSON result', result);
    });
  };
  console.log('E', response.statusCode, response.statusMessage);  
};
request(options, callback);
jtlindsey answered 2019-09-09T02:32:43Z
13 votes

我设法使用soap,wsdl和Node.js您需要安装bmicalculator.wsdl的肥皂

创建一个名为bmicalculator.wsdl的节点服务器,该服务器将定义远程客户端要使用的soap服务。 该肥皂服务基于重量(kg)和身高(m)计算体重指数。

const soap = require('soap');
const express = require('express');
const app = express();
/**
 * this is remote service defined in this file, that can be accessed by clients, who will supply args
 * response is returned to the calling client
 * our service calculates bmi by dividing weight in kilograms by square of height in metres
 */
const service = {
  BMI_Service: {
    BMI_Port: {
      calculateBMI(args) {
        //console.log(Date().getFullYear())
        const year = new Date().getFullYear();
        const n = args.weight / (args.height * args.height);
        console.log(n);
        return { bmi: n };
      }
    }
  }
};
// xml data is extracted from wsdl file created
const xml = require('fs').readFileSync('./bmicalculator.wsdl', 'utf8');
//create an express server and pass it to a soap server
const server = app.listen(3030, function() {
  const host = '127.0.0.1';
  const port = server.address().port;
});
soap.listen(server, '/bmicalculator', service, xml);

接下来,创建一个bmicalculator.wsdl文件,该文件将使用由server.js定义的soap服务。此文件将为soap服务提供参数,并使用SOAP的服务端口和端点调用url。

const express = require('express');
const soap = require('soap');
const url = 'http://localhost:3030/bmicalculator?wsdl';
const args = { weight: 65.7, height: 1.63 };
soap.createClient(url, function(err, client) {
  if (err) console.error(err);
  else {
    client.calculateBMI(args, function(err, response) {
      if (err) console.error(err);
      else {
        console.log(response);
        res.send(response);
      }
    });
  }
});

您的wsdl文件是基于xml的数据交换协议,用于定义如何访问远程Web服务。 请拨打您的wsdl文件bmicalculator.wsdl

<definitions name="HelloService" targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl" 
  xmlns="http://schemas.xmlsoap.org/wsdl/" 
  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
  xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <message name="getBMIRequest">
    <part name="weight" type="xsd:float"/>
    <part name="height" type="xsd:float"/>
  </message>

  <message name="getBMIResponse">
    <part name="bmi" type="xsd:float"/>
  </message>

  <portType name="Hello_PortType">
    <operation name="calculateBMI">
      <input message="tns:getBMIRequest"/>
      <output message="tns:getBMIResponse"/>
    </operation>
  </portType>

  <binding name="Hello_Binding" type="tns:Hello_PortType">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="calculateBMI">
      <soap:operation soapAction="calculateBMI"/>
      <input>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/>
      </input>
      <output>
        <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/>
      </output>
    </operation>
  </binding>

  <service name="BMI_Service">
    <documentation>WSDL File for HelloService</documentation>
    <port binding="tns:Hello_Binding" name="BMI_Port">
      <soap:address location="http://localhost:3030/bmicalculator/" />
    </port>
  </service>
</definitions>

希望能帮助到你

Kim .J answered 2019-09-09T02:33:29Z
9 votes

根据您需要的端点数量,可能更容易手动执行。

我已经尝试了10个库“soap nodejs”我终于手动完成了。

  • 使用节点请求([https://github.com/mikeal/request)]形成输入xml消息以将请求发送(POST)到Web服务
  • 使用xml2j([https://github.com/Leonidas-from-XIV/node-xml2js])来解析响应
dam1 answered 2019-09-09T02:34:28Z
8 votes

我成功地使用了“soap”软件包([https://www.npmjs.com/package/soap))10多个跟踪WebApis(Tradetracker,Bbelboon,Affilinet,Webgains,...)。

问题通常来自这样一个事实,即程序员不会对远程API需要进行多少调查才能进行连接或验证。

例如,PHP会自动从HTTP头重新发送cookie,但是当使用'node'包时,必须明确设置(例如'soap-cookie'包)...

smentek answered 2019-09-09T02:35:07Z
5 votes

你也可以看一下easysoap npm - [https://www.npmjs.org/package/easysoap] - 或 - 其中一些:[https://nodejsmodules.org/tags/soap]

[https://nodejsmodules.org/pkg/express-soap2json]

euroblaze answered 2019-09-09T02:35:41Z
5 votes

我使用节点网络模块打开web服务的套接字。

/* on Login request */
socket.on('login', function(credentials /* {username} {password} */){   
    if( !_this.netConnected ){
        _this.net.connect(8081, '127.0.0.1', function() {
            logger.gps('('+socket.id + ') '+credentials.username+' connected to: 127.0.0.1:8081');
            _this.netConnected = true;
            _this.username = credentials.username;
            _this.password = credentials.password;
            _this.m_RequestId = 1;
            /* make SOAP Login request */
            soapGps('', _this, 'login', credentials.username);              
        });         
    } else {
        /* make SOAP Login request */
        _this.m_RequestId = _this.m_RequestId +1;
        soapGps('', _this, 'login', credentials.username);          
    }
});

发送肥皂请求

/* SOAP request func */
module.exports = function soapGps(xmlResponse, client, header, data) {
    /* send Login request */
    if(header == 'login'){
        var SOAP_Headers =  "POST /soap/gps/login HTTP/1.1\r\nHost: soap.example.com\r\nUser-Agent: SOAP-client/SecurityCenter3.0\r\n" +
                            "Content-Type: application/soap+xml; charset=\"utf-8\"";        
        var SOAP_Envelope=  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                            "<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:SOAP-ENC=\"http://www.w3.org/2003/05/soap-encoding\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:n=\"http://www.example.com\"><env:Header><n:Request>" +
                            "Login" +
                            "</n:Request></env:Header><env:Body>" +
                            "<n:RequestLogin xmlns:n=\"http://www.example.com.com/gps/soap\">" +
                            "<n:Name>"+data+"</n:Name>" +
                            "<n:OrgID>0</n:OrgID>" +                                        
                            "<n:LoginEntityType>admin</n:LoginEntityType>" +
                            "<n:AuthType>simple</n:AuthType>" +
                            "</n:RequestLogin></env:Body></env:Envelope>";

        client.net.write(SOAP_Headers + "\r\nContent-Length:" + SOAP_Envelope.length.toString() + "\r\n\r\n");
        client.net.write(SOAP_Envelope);
        return;
    }

解析soap响应,我使用了模块 - xml2js

var parser = new xml2js.Parser({
    normalize: true,
    trim: true,
    explicitArray: false
});
//client.net.setEncoding('utf8');

client.net.on('data', function(response) {
    parser.parseString(response);
});

parser.addListener('end', function( xmlResponse ) {
    var response = xmlResponse['env:Envelope']['env:Header']['n:Response']._;
    /* handle Login response */
    if (response == 'Login'){
        /* make SOAP LoginContinue request */
        soapGps(xmlResponse, client, '');
    }
    /* handle LoginContinue response */
    if (response == 'LoginContinue') {
        if(xmlResponse['env:Envelope']['env:Body']['n:ResponseLoginContinue']['n:ErrCode'] == "ok") {           
            var nTimeMsecServer = xmlResponse['env:Envelope']['env:Body']['n:ResponseLoginContinue']['n:CurrentTime'];
            var nTimeMsecOur = new Date().getTime();
        } else {
            /* Unsuccessful login */
            io.to(client.id).emit('Error', "invalid login");
            client.net.destroy();
        }
    }
});

希望它可以帮助某人

Vince Lowe answered 2019-09-09T02:36:25Z
0 votes

添加到Kim .J的解决方案:您可以添加preserveWhitespace=true以避免Whitespace错误。 像这样:

soap.CreateClient(url,preserveWhitespace=true,function(...){
J.Aliaga answered 2019-09-09T02:36:51Z
0 votes

你也可以使用wsdlrdr。 EasySoap基本上是用一些额外的方法重写wsdlrdr。请注意,easysoap没有wsdlrdr提供的getNamespace方法。

user10152282 answered 2019-09-09T02:37:18Z
translate from https://stackoverflow.com:/questions/8655252/node-js-how-to-consume-soap-xml-web-service