2012-08-28 59 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