javascript - nodeJs soap 库无法将请求发送到 wsdl url

标签 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);

        });
    });

然后我得到这样的错误:

Error: ENOENT: no such file or directory, open '‫‪https:///example.com//TokenService?wsdl‬‬'
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '‫‪https://example.com/TokenService?wsdl‬`

我的客户是不设防的。

最佳答案

我想你快到了。以下是您可以测试的调用示例:

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 库不喜欢这个网络服务! 但是您可以自己在 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 安装请求顺便说一句来获取请求库。

关于javascript - nodeJs soap 库无法将请求发送到 wsdl url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48559901/

相关文章:

Javascript - 在函数中使用变量

javascript - ExtJS Controller 问题。未捕获的类型错误 : Object [object Object] has no method 'setSortState'

javascript - Gulp:编译 Mustache 时避免中间文件

java - cxf-rt-frontend-jaxws 的 Maven 依赖项破坏了现有的 SOAP 客户端

java - Apache CXF 异常 : java. lang.RuntimeException:找不到地址的管道启动器

javascript - window.close(), self.close() 不适用于 mozilla firefox

javascript - 是否可以在js中覆盖CSS

node.js - 如何使用 node-postgres 插入多行?

node.js - Jade 模板 - 没有图像出现@ res.render()

python - 无法使用 SOAPpy 调用 Web 服务方法