使用restclient或postman发送gzip数据

问题描述 投票:3回答:1

如何使用restclient或postman将gzip数据作为正文的一部分发送?我用apache restclient 3.2.2也无法得到响应。我附上图片供参考。

基本上我有一个xml文件,想先将其转换为gzip然后作为body的一部分发送。

要转换为GZip,我使用在线工具首先将我的xml文件转换为gzip,并将转换后的gzip文件作为body的一部分包含在我的restclient中。

我得到了以下java代码和代码我得到了正确的响应。但是无法让它在restclient工具中运行! ! restclient body

    URL url = new URL(String.format("%s?tid=%d",
                sessionInfo._getMessagesUrl, tpegId));
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/octet-stream");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // Convert XML to initial binary payload.
        byte[] bytesToSend = getMessagesRequestXml.getBytes("UTF-8");
        String fileName = "C:\\\\Sanjay\\\\Work\\\\17MM\\\\MM17_body.txt";
        if (this._outputFilename != null) {
            System.out.println(String.format("\nWriting body file '%s'", fileName));
            FileOutputStream s = new FileOutputStream(fileName);
            s.write(bytesToSend);
            s.close();
        }
        dumpBinary("Original GetMessages request", bytesToSend);

        // Optionaly compress
        if (this._shouldCompress) {
            byte[] gzippedBytes = compressToGzip(bytesToSend);
            bytesToSend = gzippedBytes;
            dumpBinary("Compressed GetMessages request", bytesToSend);
        }

        // Optionally encrypt
        if (sessionInfo._encryptionKey != null) {
            byte[] encryptedBytes = encrypt(bytesToSend, sessionInfo._encryptionKey);
            bytesToSend = encryptedBytes;
            dumpBinary("Encrypted GetMessages request", bytesToSend);
        }

        // Send request
        System.out.println(String.format("Sending GetMessages Request: %s\n", url.toString()));         
        DataOutputStream os = new DataOutputStream(
                connection.getOutputStream());
        os.write(bytesToSend, 0, bytesToSend.length);
        os.flush();
        os.close();
java rest gzip rest-client
1个回答
0
投票

我想你需要在restclient中将gzip作为文件体传递。这解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.