nodeJs soap libarary无法向wsdl url

时间:2018-02-01 09:58:24

标签: javascript node.js soap wsdl

我有一个像https://example.com/TokenService?wsdl这样的SOAP API, 我发送请求在以下函数中创建客户端

let soap = require('soap');
   soap.createClient(url, function(err, client) {
       console.log('err is ',err) ;
        client.myFunction(input_params, function(err, result) {
            console.log(result);

        });
    });

然后我得到这样的错误:

  

错误:ENOENT:没有此类文件或目录,请打开“https:///example.com//TokenService?wsdl”     错误:-2,
    代码:'ENOENT',
    系统调用:'打开',
    路径:'https://example.com/TokenService?wsdl`

我的客户端是不可取的。

1 个答案:

答案 0 :(得分:3)

我认为你几乎就在那里。以下是您可以测试的电话示例:

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肥皂库不喜欢这个网络服务! 但是您可以自己填写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安装请求BTW来获取请求库。