node.js - 如何在 node.js 中使用 MTOM SOAP Web 服务?

标签 node.js soap-client mtom node.js-stream binarystream

我需要从 node.js 中基于 soap 的 Web 服务下载或处理文件。 有人可以建议我如何在 node.js 中处理这个

我尝试使用“node-soap”或“soap”NPM 模块。它适用于普通的 soap 网络服务。但是,不适用于二进制流或基于 MTOM 的 SOAP Web 服务

最佳答案

我想尝试回答这个问题......很有趣的是,2 年零 2 个月后我仍然无法弄清楚如何轻松解决同样的问题。

我正在尝试从以下回复中获取附件:

...

headers: { 'cache-control': 'no-cache="set-cookie"', 'content-type': 'multipart/related;boundary="----=_Part_61_425861994.1525782562904";type="application/xop+xml";start="";start-info="text/xml"',

...

body: '------=_Part_61_425861994.1525782562904\r\nContent-Type: application/xop+xml; charset=utf-8; type="text/xml"\r\nContent-Transfer-Encoding: 8bit\r\nContent-ID: \r\n\r\n....\r\n------=_Part_61_425861994.1525782562904\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: binary\r\nContent-ID: \r\n\r\n�PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0002,\u0000\u0000\u0005�\b\u0006\u0........binary....

我试过了 ws.js但没有解决方案。

我的解决方案:

var request = require("request");
var bsplit = require('buffer-split')

// it will extract "----=_Part_61_425861994.1525782562904" from the response
function getBoundaryFromResponse(response) {
    var contentType = response.headers['content-type']
    if (contentType && contentType.indexOf('boundary=') != -1 ) {
        return contentType.split(';')[1].replace('boundary=','').slice(1, -1)
    }
    return null
}


function splitBufferWithPattern(binaryData, boundary) {
    var b = new Buffer(binaryData),
        delim = new Buffer(boundary),
        result = bsplit(b, delim);
    return result
}


var options = {
    method: 'POST',
    url: 'http://bla.blabal.../file',
    gzip: true,
    headers: {
        SOAPAction: 'downloadFile',
        'Content-Type': 'text/xml;charset=UTF-8'
    },
    body: '<soapenv: ... xml request of the file ... elope>'
};

var data = [];
var buffer = null;
var filename = "test.png"

request(options, function (error, response, body) {
        if (error) throw new Error(error);

        if (filename && buffer) {
            console.log("filename: " + filename)
            console.log(buffer.toString('base64'))
            // after this, we can save the file from base64 ...
        }
    })
    .on('data', function (chunk) {
        data.push(chunk)

    })
    .on('end', function () {
        var onlyPayload = splitBufferWithPattern(Buffer.concat(data), '\r\n\r\n') // this will get from PNG
        buffer = onlyPayload[2]
        buffer = splitBufferWithPattern(buffer, '\r\n-')[0]
        console.log('Downloaded.');

    })

我不确定它是否适用于大多数情况。在我看来,它看起来像是不稳定的代码,所以我正在寻找更好的东西。

关于node.js - 如何在 node.js 中使用 MTOM SOAP Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35655959/

相关文章:

javascript - 如何使用 Telegraf API 发送本地存储的照片?

JavaScript 和 Node.JS——无法理解为什么回调中的变量未定义

javascript - 用头像表达

java - SOAP、WCF 和消息签名

PHP 5.3.6 SoapClient::__doRequest():SSL:连接由对等方重置

java - 来自现有 wsdl 的带有 spring-boot 和 mtom 的 SOAP 服务

javascript - 从 MS Sql 请求表 - Node.js Web 服务

java - 如何创建 SOAP 客户端?

java - axis1.4客户端使用Mtom发送文件

java - Tomcat 6 XFire MTOM 网络服务问题