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

时间:2017-03-16 12:20:47

标签: node.js web-services soap node-soap

我正在尝试使用node-soap module使用SOAP Web服务。但是,在调用Web服务的某个方法时,我得到 '无法解析响应' 错误。

以下是实施:

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);
    }
});

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

我弄明白了这个问题。当Web服务期望 application / soap + xml 内容类型时,内容类型为 text / xml 。所以我在 createClient()参数中添加了 forceSoap12Headers:true 来强制node-soap使用SOAP 1.2版本。此外,我将 ws-addressing 标头添加到soap标头因为由于EndpointDispatcher上的AddressFilter不匹配错误,因此无法在接收方处理带有To'的消息

整体代码:

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);
    }
});

答案 1 :(得分:0)

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

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);
}
});

答案 2 :(得分:0)

我更新了npm soap版本,它对我有用