Node.js 使用 soap 模块请求 WebService 服务接口

原文:https://www.cnblogs.com/wx1993/p/6090414.html

项目开发中需要请求webservice服务,前端主要使用node.js 作为运行环境,因此可以使用soap进行请求。

使用SOAP请求webservice服务的流程如下:

1、进入项目目录,安装 soap 模块

> npm install soap --save-dev

2、在项目的 node_modules 目录下找到soap模块下的 lib > client.js,

修改代码:

soapAction = ((ns.lastIndexOf("/") !== ns.length - 1) ? ns + "/" : ns) + name;

为:

soapAction = method.soapAction || (((ns.lastIndexOf('/') !== ns.length - 1) ? ns + '/': ns) + name);

 3、请求代码

复制代码

 1   var soap = require('soap');
 2   var url = 'http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl';
 3   var args = { byProvinceName: '浙江'};
 4   soap.createClient(url, function(err, client) {
 5     client.getSupportCity(args, function(err, result) {
 6       if (err) {
 7         console.log(err);
 8       }else {
 9         console.log(result);
10       }  
11     });
12   });

复制代码

4、运行代码,在命令行窗口查看结果

除了soap模块,还有strong-soap, easysoap 等模块都可以请求webservice服务。使用方法类似。

关于webservice、SOAP、WSDL的相关知识,可以查看以下链接:

http://blog.csdn.net/u014511737/article/details/46986389

http://www.jianshu.com/p/5443f90e36de

https://segmentfault.com/a/1190000006807566

http://download.csdn.net/detail/zxktxj/8643733

猜你喜欢

转载自blog.csdn.net/zdhsoft/article/details/89704196