使用 node.js 使用 SOAP 服务时无法解析响应错误

Cannot parse response error while consuming SOAP service with node.js

我正在尝试使用 node-soap module 使用 SOAP Web 服务。但是,我在调用 Web 服务的方法之一时收到 'Cannot parse response' 错误。

实现如下:

var soap = require('soap');
var url = 'http://myservice.com/MyService.svc?wsdl';
var args = {
    Username: '***',
    Password: '***'
};

soap.createClient(url, function(err, client) {
    if (!err) {      
        client.MyService(args, function(err, response) {
            if (!err) {
                console.log('MyService response:', response);
            } else {
                console.log('Error in MyService:', err);
            }
        });
    } else {
        console.log('Error in createClient: ', err);
    }
});

我该如何解决这个问题?

我找到问题了。虽然 Web 服务需要 application/soap+xml 内容类型,但内容类型是 text/xml。所以我在 createClient() 参数中添加了 forceSoap12Headers: true 以强制 node-soap 使用 SOAP 1.2 版本。此外,我将 ws-addressing headers 添加到 soap header 因为 The message with To ' ' cannot be processed at接收器,由于 EndpointDispatcher 错误中的 AddressFilter 不匹配。

整体代码:

var soap = require('soap');
var url = 'http://myservice.com/MyService.svc?wsdl';
var args = {
    Username: '***',
    Password: '***'
};
var soapOptions = {
    forceSoap12Headers: true
};
var soapHeaders = {
    'wsa:Action': 'http://tempuri.org/MyPortName/MyAction',
    'wsa:To': 'http://myservice.com/MyService.svc'
};

soap.createClient(url, soapOptions, function(err, client) {
    if (!err) {
        client.addSoapHeader(soapHeaders, '', 'wsa', 'http://www.w3.org/2005/08/addressing');
        client.MyService(args, function(err, response) {
            if (!err) {
                console.log('MyService response:', response);
            } else {
                console.log('Error in MyService:', err);
            }
        });
    } else {
        console.log('Error in createClient: ', err);
    }
});

添加此代码 client.setSecurity(新soap.BasicAuthSecurity('username', 'password')); 在您创建客户端之后。它对我有用:

var soap = require('soap');
var url = 'http://myservice.com/MyService.svc?wsdl';
soap.createClient(url, function(err, client) {
if (!err) {      
      client.setSecurity(new soap.BasicAuthSecurity('username', 'password'));
      client.MyService(args, function(err, response) {
      if (!err) {
            console.log('MyService response:', response);
        } else {
            console.log('Error in MyService:', err);
        }
    });
} else {
    console.log('Error in createClient: ', err);
}
});

我已经更新了 npm soap 版本并且对我有用