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

时间:2016-02-26 15:43:56

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

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

中处理这个问题

我试过'node-soap'或'soap'NPM模块。它适用于普通的肥皂网服务。但是,不是基于二进制蒸汽或基于MTOM的SOAP Web服务

2 个答案:

答案 0 :(得分:0)

我想尝试回答这个问题......很有意思的是,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:   应用/ XOP + xml的;字符集= UTF-8;   type =“text / xml”\ r \ nConContent-Transfer-Encoding:8bit \ r \ nContent-ID:   \ r \ n \ r \ n .... \ r \ n ------ = _ Part_61_425861994.1525782562904 \ r \ nContent型:   应用/八位字节流\ r \ nContent传送编码:   二进制\ r \ nContent-ID:   \ r \ n \ r \ nPNG \ 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.');

    })

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

答案 1 :(得分:0)

使用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::*"