我正在尝试使用Node-Soap模块消费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);
}
});
我该如何解决?
我发现了问题。当Web服务期望应用程序/SOAP XML 内容类型时,内容类型为 text/xml 。因此,我添加了 worceoap12headers:true 在 createClient()参数中迫使节点soap使用SOAP 1.2版本。另外,由于,由于eNdpointDispatcher erory的地址不匹配,我无法在接收器上处理,因此我将 ws-ws-addressing 标头添加到肥皂标头。。
总体代码:
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'));创建客户端后。它对我有用:
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版本,它对我有用