简体   繁体   English

nodeJs soap libarary无法向wsdl url发送请求

[英]nodeJs soap libarary can not send request to wsdl url

I Have a SOAP API like https://example.com/TokenService?wsdl , i send a request to create Client in below function : 我有一个像https://example.com/TokenService?wsdl这样的SOAP API,我在下面的函数中发送创建Client的请求:

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

        });
    });

Then i get an error like this: 然后我得到这样的错误:

Error: ENOENT: no such file or directory, open '‫‪ https:///example.com//TokenService?wsdl ‬‬' 错误:ENOENT:没有这样的文件或目录,打开' https:///example.com//TokenService?wsdl '
errno: -2, 错误:-2,
code: 'ENOENT', 代码:'ENOENT',
syscall: 'open', 系统调用:'打开',
path: '‫‪ https://example.com/TokenService?wsdl ‬` 路径:' https : //example.com/TokenService? wsdl`

And my client is undefiend. 而我的客户是不妥协的。

I think you're very nearly there. 我觉得你差不多了。 Here's an example of a call you can test: 以下是您可以测试的呼叫示例:

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);
        });
    }
});

I think you may just need to pass two urls to the function, the service url and the WSDL url. 我想你可能只需要将两个url传递给函数,服务url和WSDL url。 Sometimes you don't need to do this and the WSDL will contain the links to all the operation URLs. 有时您不需要这样做,WSDL将包含指向所有操作URL的链接。 Some calls will work if you omit the {endpoint: url } option, and some won't. 如果省略{endpoint:url}选项,某些调用将起作用,而某些调用则不会。

Also check the WSDL link, ensure the WSDL is valid, you can look at it in your browser, it should look like this: 还要检查WSDL链接,确保WSDL有效,您可以在浏览器中查看它,它应如下所示:

http://www.webservicex.net/globalweather.asmx?WSDL http://www.webservicex.net/globalweather.asmx?WSDL

You can also download the WSDL to a file and try that: 您还可以将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);
        });
    }
});

You need to put the WSDL file in the same directory as your script. 您需要将WSDL文件放在与脚本相同的目录中。

I get can this working in a C# client, the XML POST looks like this: 我知道这可以在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>

And the response: 并回应:

<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>

I can do a POST to the address by doing the following: 我可以通过执行以下操作对该地址执行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);
});

For some reason the NPM soap library does not like this webservice! 由于某种原因,NPM肥皂库不喜欢这个网络服务! But you could fill in the values in the XML yourself, it might get you a bit further if you have the details you need to fill. 但是您可以自己填写XML中的值,如果您需要填写详细信息,它可能会让您更进一步。

I'm getting the response: 我收到了回复:

<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>

But I don't the the username password maybe. 但我不是用户名密码。 If you can fill in these details maybe you will get a successful response. 如果您可以填写这些详细信息,那么您将获得成功的回复。 You need to do an NPM install request BTW to get the request library. 您需要执行NPM安装请求BTW才能获取请求库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM