Node JS Soap使用CopyIntoItems将文件发送到基于sharepoint的webserver

时间:2018-03-29 07:59:54

标签: sharepoint proxy node-soap

我正在使用Node-Soap模块编写Node JS SOAP客户端,以将文件发送到基于SharePoint的远程Web服务。

计算机客户端需要代理才能访问Internet,而SharePoint WS需要帐户(user,pwd)。以下是代码源。

但是,我总是有错误“(节点:20857)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):错误:无法解析响应”。

有人可以帮助我吗?

var process = require('process');
var fs = require('fs');
var request = require('request')
var soap = require('soap');
var apiWSDL = '.../test-collab/WS/_vti_bin/copy.asmx?wsdl';


function sendFile() {

var p = new Promise(function (resolve, reject) {

    request_with_defaults = request.defaults({
        'proxy': 'http://***:***@10.115.108.109:8080',
        'timeout': 50000,
        'connection': 'keep-alive'
    });


    var options = {
        'request': request_with_defaults,
        endpoint: 'https://.../test-collab/WS/_vti_bin/copy.asmx',
    }

    var byteArray = fs.readFileSync('test.txt').toString('base64');

    process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    //process.env.https_proxy = 'http://***@***:10.115.108.109:8080';

    soap.createClient(apiWSDL, options, function (err, client) {
        if (err) throw new Error(err);

        var args = {
            DestinationUrls: 'https://.../test-collab/WS/CAS/test.txt',
            Stream: byteArray
        }

        client.setSecurity(new soap.ClientSSLSecurity(null, null, null, {   /*default request options like */
            strictSSL: false,
            rejectUnauthorized: false,
            // hostname: 'some-hostname'
            //secureOptions: constants.SSL_OP_NO_TLSv1_2,
            forever: true,
        }));

        client.addHttpHeader('vm6_webapp', 'SERVICE');
        client.addHttpHeader('vm6_password', '***');
        client.addHttpHeader('vm6_user', '***');
        client.CopyIntoItems(args, function (err, result) {
            //console.log(err);
            if (err) {
                console.log(err);
                reject(err);
            }
            var sets;
            try {
                console.log(result);
                if (result.length) {
                    resolve(result);
                } else {
                    reject(result);
                }
            } catch (error) {
                console.log("error");
                reject("error und")
            }



        });
    });


});

return p;

}

2 个答案:

答案 0 :(得分:0)

由于错误消息为var introguide = introJs(); window.addEventListener('load', function () { var doneTour = localStorage.getItem('MyTour') === 'Completed'; if (doneTour) { return; } else { introguide.start() introguide.oncomplete(function () { localStorage.setItem('MyTour', 'Completed'); }); introguide.onexit(function () { localStorage.setItem('MyTour', 'Completed'); }); } }); ,因此可能会出现两种可能性:

  • 响应不是XML格式
  • 或wsdl中定义的SOAP响应消息不接受XML响应。

您可以使用现有客户端重做SOAP请求,例如,SoapUI确认吗?

否则,我建议使用Cannot parse response而不是console.error( err.stack )来获取console.log( err )的完整执行跟踪。

答案 1 :(得分:0)

感谢Nghia的回复。

事实上,我之前为这个WebServers编写了一个Java客户端,它可以工作。这意味着参数是正常的,XML响应也可以。

以下是Java中的代码:

 MoccaClient clientWSMocca = new MoccaClient();
        CopySoap copySoap = (CopySoap)clientWSMocca.getClient(site_soap_url,
                proxy_host, proxy_port, proxy_user, proxy_password,
                mocca_user, mocca_password, mocca_web_app,
                CopySoap.class);

        // Récupération sous forme de tableau de bytes du fichier
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {

            InputStream in = new BufferedInputStream(new FileInputStream(file_path));

            BufferedOutputStream bufOut = new BufferedOutputStream(out);
            for (int b = in.read(); b != -1; b = in.read()) {
                bufOut.write(b);
            }
            in.close();
            bufOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Initialisation des variables de contexte
        FieldInformation fieldInformation = new FieldInformation();

        String destinationUrl = site_url + file_name;

        DestinationUrlCollection destinationUrlCollection = new DestinationUrlCollection();
        destinationUrlCollection.getString().add(destinationUrl);
        FieldInformationCollection fieldInformationCollection = new FieldInformationCollection();
        fieldInformationCollection.getFieldInformation().add(fieldInformation);
        Holder<CopyResultCollection> copyResult= new Holder<CopyResultCollection>();
        Holder<Long> getItemResult = new Holder<Long>();

        copySoap.copyIntoItems(file_path, destinationUrlCollection, fieldInformationCollection, out.toByteArray(), getItemResult, copyResult);

MoccaClient.java

public class MoccaClient {
    public Object getClient(String url,
                            String proxyhost, int proxyport, String userproxy, String passwordproxy,
                            String moccauser, String moccapassword, String moccawebapp,
                            Class<?> serviceclass) {
        System.setProperty("org.apache.cxf.JDKBugHacks.defaultUsesCaches", "true");
        boolean bssl = false;
        if (url.startsWith("https")) {
            bssl = true;
        }

        if (url.startsWith("HTTPS")) {
            bssl = true;
        }

        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.getInInterceptors().add(new LoggingInInterceptor());
        factory.getOutInterceptors().add(new LoggingOutInterceptor());
        factory.getInInterceptors().add(new MyInterceptor());
        factory.setServiceClass(serviceclass);
        factory.setAddress(url);
        Object client = factory.create();
        Client clientDuProxy = ClientProxy.getClient(client);
        Map<String, List<String>> headers = new HashMap();
        headers.put("vm6_user", Arrays.asList(moccauser));
        headers.put("vm6_password", Arrays.asList(moccapassword));
        headers.put("vm6_webapp", Arrays.asList(moccawebapp));
        clientDuProxy.getRequestContext().put(Message.PROTOCOL_HEADERS, headers);
        HTTPConduit http = (HTTPConduit)clientDuProxy.getConduit();
        HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
        http.setClient(httpClientPolicy);
        if (proxyhost != null) {
            http.getClient().setProxyServer(proxyhost);
            http.getClient().setProxyServerPort(proxyport);
        }

        if (userproxy != null) {
            http.getProxyAuthorization().setUserName(userproxy);
            http.getProxyAuthorization().setPassword(passwordproxy);
        }

        if (bssl) {
            TrustManager[] trustCerts = new TrustManager[]{new AllTrust()};
            TLSClientParameters tcp = new TLSClientParameters();
            tcp.setTrustManagers(trustCerts);
            tcp.setSecureSocketProtocol("TLS");
            tcp.setDisableCNCheck(true);
            http.setTlsClientParameters(tcp);
        }

        return client;
    }
}