在使用node.js使用SOAP服务时,无法parsing响应错误

我正在尝试使用node-soap模块来使用SOAP Web服务。 但是,在调用Web服务的一种方法时,出现“无法parsing响应”错误。

这是实现:

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

我怎样才能解决这个问题?

我解决了这个问题。 当期望application / soap + xml内容types的web服务时,内容types是text / xml 。 所以我在createClient()参数中添加了forceSoap12Headers:true ,强制node-soap使用SOAP 1.2版本。 另外,我添加了ws-addressing标题给肥皂标题,因为To'的消息不能在接收方处理,因为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(new soap.BasicAuthSecurity('username','password')); 在你创build客户端之后。 它为我工作:

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