簡體   English   中英

nodeJs soap libarary無法向wsdl url發送請求

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

我有一個像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);

        });
    });

然后我得到這樣的錯誤:

錯誤:ENOENT:沒有這樣的文件或目錄,打開' https:///example.com//TokenService?wsdl '
錯誤:-2,
代碼:'ENOENT',
系統調用:'打開',
路徑:' 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肥皂庫不喜歡這個網絡服務! 但是您可以自己填寫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才能獲取請求庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM