2012-08-28 82 views
0

我知道類似的問題發佈在這裏:Android Session cookies without using CookieManager 但是,我無法讓它工作。沒有CookieManager的Android餅乾

URL urlObj = new URL(urlPath); 
    conn = (HttpURLConnection) urlObj.openConnection(); 

    if (urlPath.toLowerCase().startsWith("https:")) { 
     initializeHttpsConnection((HttpsURLConnection) conn); 
    } 
    conn.setRequestMethod("POST"); 


     conn.setDoInput(true); 
     conn.setDoOutput(true); 
     conn.connect(); 
     // Send body data 
     os = conn.getOutputStream(); 
     os.write(bodyData); 
     // Must flush and close to make sure all the data is sent. 
     os.flush(); 
     os.close(); 
     // Get response 
     in = conn.getInputStream(); 
     System.out.println("Initial set of cookies:"); 

     String cookie = conn.getRequestProperty("Cookie"); 

// Map> rp = conn.getRequestProperties(); (cookie!= null & & cookie.length()> 0) { _cookie = cookie; Log.v(「cookie2」,_cookie); }

「cookie」始終爲空。但是,如果我確實包含了CookieManger(並以2.3.3+運行它),那麼cookie就具有所需的值。

回答

1

嘗試一下本作HTTP URL CON:

while ((headerfields = connection.getHeaderField(i)) != null) { 
      String key = connection.getHeaderFieldKey(i); 
      if (key.equalsIgnoreCase("SET-COOKIE")) { 
     ssss= (((key==null) ? "" : key + ": ") + headerfields);} 
      i++; 

      } 
+0

我希望在 「SSSS」 什麼? – theblitz

+0

其存儲cookie字符串 –

+0

太好了。所以,結果是:Set-Cookie:JSESSIONID = 3B299C570A21D2A5E84673AC7718BA1C; Path =/cell在執行setRequestProperty時,我需要「發回」哪些內容? – theblitz