【问题标题】:How to consume MTOM SOAP web service in node.js?如何在 node.js 中使用 MTOM SOAP Web 服务?
【发布时间】:2016-06-09 22:03:19
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

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

    我正在尝试从以下响应中获取附件:

    ...

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

    ...

    正文:'------=_Part_61_425861994.1525782562904\r\n内容类型: 应用程序/xop+xml;字符集=utf-8; type="text/xml"\r\nContent-Transfer-Encoding: 8bit\r\nContent-ID: \r\n\r\n....\r\n------=_Part_61_425861994.1525782562904\r\n内容类型: 应用程序/八位字节流\r\n内容传输编码: 二进制\r\n内容 ID: \r\n\r\n�PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR\u0000\u0000\u0002,\u0000\u0000\u0005...\b\u0006\u0.... ....二进制....

    我试过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.');
    
        })
    

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

    【讨论】:

    • 很难(对我来说)判断这是一个答案还是另一个问题。
    【解决方案2】:

    使用ws.js

    以下是获取文件附件的方法:

    const ws = require('ws.js')
    const { Http, Mtom } = ws
    
    var handlers =  [ new Mtom(), new Http()];
    var request = '<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">' +
                    '<s:Body>' +
                      '<EchoFiles xmlns="http://tempuri.org/">' +
                          '<File1 />' +
                      '</EchoFiles>' +
                    '</s:Body>' +
                  '</s:Envelope>'
    
    var ctx = { request: request
              , contentType: "application/soap+xml"
              , url: "http://localhost:7171/Service/mtom"
              , action: "http://tempuri.org/IService/EchoFiles"
              }
    
    ws.send(handlers, ctx, function(ctx) {
      //read an attachment from the soap response
      var file = ws.getAttachment(ctx, "response", "//*[local-name(.)='File1']")
      // work with the file
      fs.writeFileSync("result.jpg", file)
    })
    

    两个限制:

    xpath = "//*[@href='cid:" + encodeURIComponent(id) + "']//parent::*"
    

    与:

    xpath = "//*[@href='cid:" + id + "']//parent::*"
    

    【讨论】:

      猜你喜欢
      • 2012-01-29
      • 2014-05-30
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 2012-09-09
      • 2021-05-27
      • 1970-01-01
      相关资源
      最近更新 更多