2015-07-12 2346 views
3

如何使用restclient或postman將gzip數據作爲身體的一部分發送?我用apache restclient 3.2.2也無法獲得響應。我附上圖片供參考。使用restclient或postman發送gzip數據

基本上我有一個xml文件,想先將它轉換爲gzip,然後作爲body的一部分發送。

要轉換爲GZip我使用的在線工具首先將我的xml文件轉換爲gzip,並將轉換的gzip文件作爲body的一部分包含在我的restclient中。

我得到以下的java代碼和代碼,我得到的響應正常。 但無法使其在restclient工具中工作!restclient headers 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(); 

回答

0

我想通了,你需要傳遞的gzip在RESTClient實現filebody。這解決了我的問題。

+0

我有一個類似的問題,但與GET方法。我正在返回一個gzip的json響應給某個GET請求。我的瀏覽器會自動解壓縮它。但郵遞員返回的東西不可靠。你有什麼想法我應該做什麼? – riroo

+0

同樣的問題在這裏,任何解決方案,以解碼郵遞員的數據? –

+0

如何將gzip作爲filebody傳遞給其他高級客戶端?謝謝 –

相關問題