2013-07-28 23 views
1

我正在實施小型Java ME應用程序。這個應用程序從第三個patty資源獲取一些數據,並需要先進行身份驗證。我首先要求獲取cookie(這很容易),並且第二次使用此cookie來獲取數據。我GOOGLE了一點怎麼辦呢,發現一個解決方案 - Deal with cookie with J2MEJava ME(Nokia Asha)中的解碼響應

我已經改變了這種代碼,以未來爲我的目的:

public void getData(String url,String cookie) { 
    HttpConnection hpc = null; 
    InputStream is = null; 

    try { 
     hpc = (HttpConnection) Connector.open(url); 
     hpc.setRequestProperty("cookie", cookie); 
     hpc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); 
     hpc.setRequestProperty("Accept-Encoding", "gzip, deflate"); 
     hpc.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 
     is = hpc.openInputStream(); 
     int length = (int) hpc.getLength(); 
     byte[] response = new byte[length]; 
     is.read(response); 
     String strResponse = new String(response); 
    } catch (Exception e) { 
     System.out.println(e.getMessage() + " " + e.toString()); 
    } finally { 
     try { 
      if (is != null) 
       is.close(); 
      if (hpc != null) 
       hpc.close(); 
     } catch (Exception e) {} 
    } 
} 

我得到類似下一

??ÑÁNÃ0à;O±(²§M}[email protected] 
.?PYS¨Ôe¥Í@\üìde??XÊo}Vâ]hk?­6ëµóA|µvÞz'Íà?wAúêmw4í0?ÐÆ?ÚMW=?òêz CÛUa:6Ö7¼T?<oF?nh6[_0?l4?äê&)?çó³?ÅÕúf¨ä(.? ªDÙ??§?ÊP+??(:?Á,Si¾ïA¥ã-jJÅÄ8ÊbBçL)gs.S.þG5ÌÀÆéX}CÁíÑ-þ?BDK`²?\¶?ó3I÷ô±e]°6¬c?q?Ó?¼?Y.¯??Y?%?ÏP1è?ìw;?È Ò??e 
|ôh0? 

我該如何解碼?

回答

0

愚蠢的我。我沒有考慮下一個代碼:hpc.setRequestProperty("Accept-Encoding", "gzip, deflate");我得到ZIP壓縮編碼,我需要的所有東西都解碼它。