如何在node.js中使用WCF soap Web服务

时间:2013-03-25 10:07:20

标签: wcf node.js soap

我尝试使用节点模块wcf.js在网络中提供了大量示例。但无法获得任何适当的结果。我正在使用以下网址

  

https://webservice.kareo.com/services/soap/2.1/KareoServices.svc?wsdl

任何能够在代码帮助下解释我的人都会非常有帮助。我想知道如何访问node.js中的wsdl

感谢。

6 个答案:

答案 0 :(得分:1)

你没有那么多选择。

你可能想要使用以下之一:

  • 节点皂
  • 冲洗
  • soapjs

我尝试使用node-soap以下列代码获得INR USD费率。

app.get('/getcurr', function(req, res) {
var soap = require('soap');
var args = {FromCurrency: 'USD', ToCurrency: 'INR'};
var url = "http://www.webservicex.net/CurrencyConvertor.asmx?WSDL";
soap.createClient(url, function(err, client) {
    client.ConversionRate(args, function(err, result) {
        console.log(result);
    });
  });
});

答案 1 :(得分:1)

代码项目有一个neat sample使用wcf.js,其中的api是wcf,所以不需要学习新的范例。

答案 2 :(得分:0)

你可能想要使用以下之一:

Aslo,有an existing question

答案 3 :(得分:0)

我认为另一种选择是:

  • 使用SoapUI等工具记录输入和输出xml消息
  • 使用node request形成输入xml消息以将请求发送(POST)到Web服务(请注意,标准javascript模板机制,例如ejsmustache可以帮助您)最后
  • 使用XML解析器将响应数据反序列化为JavaScript对象

是的,这是一个相当肮脏和低级别的方法,但它应该没有问题

答案 4 :(得分:0)

请查看wcf.js

简而言之,您可以按照以下步骤操作:

  1. npm install wcf.js

  2. 编写如下代码:

  3. 代码

    var Proxy = require('wcf.js').Proxy; 
    var BasicHttpBinding = require('wcf.js').BasicHttpBinding; 
    
    var binding = new BasicHttpBinding();
    
    //Ensure the proxy variable created below has a working wsdl link that actually loads wsdl    
    var proxy = new Proxy(binding, "http://YourHost/YourService.svc?wsdl");
    
    /*Ensure your message below looks like a valid working SOAP UI request*/
    var message = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:sil='http://YourNamespace'>" +
                    "<soapenv:Header/>" +
                    "<soapenv:Body>" +
                    "<sil:YourMethod>" +
                    "<sil:YourParameter1>83015348-b9dc-41e5-afe2-85e19d3703f9</sil:YourParameter1>" +
                    "<sil:YourParameter2>IMUT</sil:YourParameter2>" +
                    "</sil:YourMethod>" +
                    "</soapenv:Body>" +
                    "</soapenv:Envelope>";
    /*The message that you created above, ensure it works properly in SOAP UI rather copy a working request from SOAP UI*/
    
    /*proxy.send's second argument is the soap action; you can find the soap action in your wsdl*/
    proxy.send(message, "http://YourNamespace/IYourService/YourMethod", function (response, ctx) {
        console.log(response);
        /*Your response is in xml and which can either be used as it is of you can parse it to JSON etc.....*/
    });
    

答案 5 :(得分:0)

就我而言,我使用了https://www.npmjs.com/package/soap。默认情况下,forceSoap12Headers选项设置为false,这阻止了node-soap根据SOAP 1.2生成正确的肥皂消息。检查更多详细信息:I am confused about SOAP namespaces。将其设置为true后,便可以调用.NET WCF服务。这是一个对我有用的TypeScript代码抓取器。

import * as soap from 'soap';
import { IOptions } from 'soap';

// ...

const url = 'https://www.your-domain.com/stock.svc?wsdl';
const opt: IOptions = {
  forceSoap12Headers: true,
};

soap.createClient(url, opt, (err, client: soap.Client) => {
  if (err) {
    throw err;
  }

  const wsSecurityOptions = {
    hasTimeStamp: false,
  };

  const wsSecurity = new soap.WSSecurity('username', 'password', wsSecurityOptions);
  client.setSecurity(wsSecurity);
  client.addSoapHeader({ Action: 'http://tempuri.org/API/GetStockDetail' }, undefined, 'wsa', 'http://www.w3.org/2005/08/addressing');
  client.addSoapHeader({ To: 'https://www.your-domain.com/stock.svc' }, undefined, 'wsa', 'http://www.w3.org/2005/08/addressing');

  const args = {
    symbol: 'GOOG',
  };

  client.GetStockDetail(
    args,
    (requestErr, result) => {
      if (requestErr) {
        throw requestErr;
      }

      console.log(result);
    },
  );
});

以下是node-soap用法文档的链接:

  1. https://github.com/vpulim/node-soap/tree/master/test
  2. https://github.com/vpulim/node-soap