2014-03-27 126 views
0

我正在修改一些工作代碼以使用其他提供者的API(我們正在切換幫助臺提供者)。HTTPURLConnection返回亂碼

我想看看xml回來看看我是否在正確的軌道上,但所有我看到回來是胡言亂語。我看過this的問題,但無法弄清楚這些答案如何適用於我的情況。

如果我沒有記錯,當我用其他的API,我能夠讀回來在控制檯這裏的XML:

while ((line = br.readLine()) != null) { 
    System.out.println(line); 
} 

我的問題是:有沒有一種方法可以讓我不同的讀取流這樣我就可以讀取返回的xml,或者我還有其他問題?

我很新這個,所以任何想法都讚賞。更多細節如下。

代碼:

package com.google.gwt.HelpDeskTest.server; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import com.google.gwt.user.server.rpc.RemoteServiceServlet; 
import com.google.gwt.HelpDeskTest.client.HelpDeskTestService; 
import com.google.gwt.HelpDeskTest.shared.HelpDeskTestException; 


@SuppressWarnings("serial") 
public class HelpDeskTestImpl extends RemoteServiceServlet implements 
    HelpDeskTestService { 

    @Override 
    public String postToRemoteServer(String serviceUrl) 
      throws HelpDeskTestException { 
     try { 

      final String serverPath= "https://www.myconnectwise.net/v4_6_release/services/system_io/integration_io/processClientAction.rails"; 

      System.out.println(serverPath); 


      final String serverParameters= "<?xml version=%221.0%22 encoding=%22utf-16%22?>" + 
      "<GetTicketAction xmlns:xsi=%22http://www.w3.org/2001/XMLSchema-instance%22 xmlns:xsd=%22http://www.w3.org/2001/XMLSchema%22>" + 
      "<CompanyName>xxxxxx</CompanyName><IntegrationLoginId>xxxxxxx</IntegrationLoginId><IntegrationPassword>xxxxxx</IntegrationPassword>" + 
      "<SrServiceRecid>1921</SrServiceRecid></GetTicketAction>"; 


      System.out.println(serverParameters); 

      //Open HttpURLConnection:   

      URL url = new URL(serverPath); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

      connection.setConnectTimeout(10000); //added this to see if I can address the timeout issue. 
      connection.setReadTimeout(10000); 

      connection.setDoOutput(true); 
      connection.setDoInput(true); 
      connection.setInstanceFollowRedirects(false); 
      connection.setRequestMethod("POST"); 
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      connection.setRequestProperty("charset", "utf-16"); 
      connection.setRequestProperty("Content-Length", "" + Integer.toString(serverParameters.getBytes().length)); 
      connection.setUseCaches (false); 
      //connection.setChunkedStreamingMode(0); 

      DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
      wr.writeBytes(serverParameters); 
      wr.flush(); 
      wr.close(); 

      //process response - need to get xml response back. 
      //this was the working line of code: 
      InputStream stream = connection.getInputStream(); 

      //put output stream into a string 
      BufferedReader br = new BufferedReader(new InputStreamReader(stream)); 
      String result = ""; 
      String line; 
      while ((line = br.readLine()) != null) { 
       System.out.println(line); 
       result+= line; 
      } 

      br.close(); 
      connection.disconnect(); 

      System.out.println(result); 

      return result; 


     } catch (final Exception e) { 
      System.out.println(e.getMessage()); 
      throw new HelpDeskTestException(); 
      //handle timeout error 

     } 
    } 
} 

這是我試圖發送XML。我已經通過公司的API測試器測試過它,並知道它可以工作,並通過發送XML來響應。

<?xml version="1.0" encoding="utf-16"?> 
<GetTicketAction xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <CompanyName>xxxxxx</CompanyName> 
    <IntegrationLoginId>xxxxxx</IntegrationLoginId> 
    <IntegrationPassword>xxxxx</IntegrationPassword> 
    <SrServiceRecid>1921</SrServiceRecid> 
</GetTicketAction> 
+0

嘗試'connection.getContent()' – anttix

回答

1

當您發送數據時,您指定utf-16爲編碼。

但是,當您讀取響應時,您不指定編碼,因此使用默認平臺編碼。

所以交流這行:

BufferedReader br = new BufferedReader(new InputStreamReader(stream)); 

本(假設響應也用UTF-16編碼):

BufferedReader br = new BufferedReader(new InputStreamReader(stream,"utf-16")); 

你實際上應該檢查響應頭,以瞭解哪些編碼具有已被使用。

+0

我會檢查響應頭,但我認爲我們是在正確的軌道上,因爲我現在當我運行出現以下錯誤:?????? .. ..Lots和很多問號...... ??????? com.google.gwt.xml.client.impl.DOMParseException:無法解析:第1行第1列出錯:文檔爲空 – brl8

+0

我在響應頭中驗證了編碼爲utf-16。我將繼續並將其標記爲答案並開始追蹤我的其他錯誤!謝謝! – brl8

0

所以經過多次搜索,我找到了答案。 xml被讀爲亂碼,因爲它是Gzip壓縮的。閱讀本文的方法是使用GZIPInputStream的 。這是因爲XML壓縮方式不同。

HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestProperty("Accept-Encoding", "gzip"); 
     InputStreamReader in = new InputStreamReader (new GZIPInputStream(connection.getInputStream())); 
     String str;    
     while (true) { 
    int ch = in.read(); 
    if (ch==-1) { 
     break; 
    }