2017-03-16 154 views
1

我試圖使用node-soap module來使用SOAP Web服務。但是,我得到'無法解析響應'錯誤,同時調用Web服務的方法之一。在使用node.js使用SOAP服務時無法解析響應錯誤

下面是執行:

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

我想出了這個問題。雖然網絡服務期望內容類型爲application/soap + xml,但內容類型爲text/xml。所以我加了forceSoap12Headers:true其中其中createClient()參數強制node-soap使用SOAP 1.2版本。另外,由於EndpointDispatcher錯誤導致AddressFilter不匹配,因此我添加了ws-addressing標頭,因爲帶有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); 
    } 
}); 
0

添加此代碼 client.setSecurity(新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); 
} 
});