我想你快到了。下面是一个可以测试的调用示例:
const soap = require('soap');
const url = 'http://www.webservicex.net/globalweather.asmx';
const wsdlURL = 'http://www.webservicex.net/globalweather.asmx?WSDL';
soap.createClient(wsdlURL, {endpoint: url}, function(error, client) {
if (error) {
console.error(error);
} else {
client.GetCitiesByCountry({ CountryName: 'Russian Federation'}, function(error, result) {
if (error) {
console.error(error);
process.exit(1);
}
console.log(result);
});
}
});
我认为您可能只需要将两个 url 传递给函数,即服务 url 和 WSDL url。有时您不需要这样做,WSDL 将包含所有操作 URL 的链接。如果您省略 {endpoint: url } 选项,有些调用会起作用,而有些则不会。
还要检查 WSDL 链接,确保 WSDL 有效,您可以在浏览器中查看,应该如下所示:
http://www.webservicex.net/globalweather.asmx?WSDL
您也可以将 WSDL 下载到文件中并尝试:
"use strict";
const wsdlFileName = 'globalweather.wsdl';
const path = require('path');
const wsdlFile = path.join(__dirname, wsdlFileName);
const soap = require('soap');
const url = 'http://www.webservicex.net/globalweather.asmx';
soap.createClient(wsdlFile, {endpoint: url}, function(error, client) {
if (error) {
console.error(error);
} else {
client.GetCitiesByCountry({ CountryName: 'Russian Federation'}, function(error, result) {
if (error) {
console.error(error);
process.exit(1);
}
console.log(result);
});
}
});
您需要将 WSDL 文件与您的脚本放在同一目录中。
我知道这可以在 C# 客户端中工作吗,XML POST 看起来像这样:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<reservation xmlns="http://token.ws.web.cnpg/">
<Token_param xmlns="">
<AMOUNT>amount</AMOUNT>
<CRN>crn</CRN>
<MID>mid</MID>
<REFERALADRESS>referaladdress</REFERALADRESS>
<SIGNATURE>signature</SIGNATURE>
<TID>tid</TID>
</Token_param>
</reservation>
</s:Body>
</s:Envelope>
然后回应:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:reservationResponse xmlns:ns2="http://token.ws.web.cnpg/">
<return>
<result>3</result>
<token>Security check was not successful.</token>
</return>
</ns2:reservationResponse>
</S:Body>
</S:Envelope>
我可以通过执行以下操作对该地址进行 POST:
var request = require('request');
var url = 'https://example.com/TokenService';
var tokenXML = `<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<reservation xmlns="http://token.ws.web.cnpg/">
<Token_param xmlns="">
<AMOUNT>amount</AMOUNT>
<CRN>crn</CRN>
<MID>mid</MID>
<REFERALADRESS>referaladdress</REFERALADRESS>
<SIGNATURE>signature</SIGNATURE>
<TID>tid</TID>
</Token_param>
</reservation>
</s:Body>
</s:Envelope>`;
console.log('Posting..')
console.log(tokenXML);
request.post(
{url: url,
body : tokenXML,
headers: {'Content-Type': 'text/xml'}
}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
由于某种原因,NPM soap 库不喜欢这个 web 服务!
但是您可以自己填写 XML 中的值,如果您有需要填写的详细信息,可能会更进一步。
我收到了回复:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:reservationResponse xmlns:ns2="http://token.ws.web.cnpg/">
<return>
<result>3</result>
<token>Security check was not successful.</token>
</return>
</ns2:reservationResponse>
</S:Body>
</S:Envelope>
但我可能没有用户名密码。如果您可以填写这些详细信息,也许您会得到成功的回复。顺便说一句,您需要执行 NPM 安装请求以获取请求库。