1

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

机器客户端需要代理才能访问 Internet,SharePoint WS 需要帐户(用户、密码)。下面是代码源。

但是,我总是遇到错误“(节点: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;

}
4

2 回答 2

0

由于错误消息是Cannot parse response,可能会发生两种可能性:

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

您能否通过使用现有客户端(例如,SoapUI 来确认)重做 SOAP 请求?

否则,我建议使用console.error( err.stack )而不是console.log( err )获取err.

于 2018-03-29T13:28:24.767 回答
0

感谢 Nghia 的回复。

事实上,我之前已经用 Java 为这个 WebServers 编写了一个 SOAP 客户端,它可以工作。这意味着参数没问题,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;
    }
}
于 2018-03-29T14:59:21.107 回答