2011-05-17 107 views
0

我目前正在Android上開發一個應用程序,並使用App Engine作爲後端。由於存在一些嚴重的解析,我有時需要將會話cookie從android設備傳遞到服務器,以便在解析響應數據時進行身份驗證。Cookie問題,請求時仍保留舊Cookie

下面的代碼是從服務器端,我把我的android設備傳遞的Cookie放在服務器HttpUrlConnection對象中。現在到我的問題。

即使我將有效的會話cookie傳遞給服務器,我仍然遇到了未經過身份驗證的巨大問題。經過幾天的調試,似乎我今天發現了這個問題,但我不知道如何解決這個問題。

使用Microsoft網絡監視器,我可以看到有時候,當我從服務器發出請求時,但並不總是,請求中有第二個cookie,BESIDES是我放入HttpUrlConnection的那個。這似乎混淆了第三方服務器,它使用Set-cookie進行響應,而不是讓我訪問安全頁面。

的餅乾看起來像這樣:

The cookie that I set 
Cookie: PHPSESSID=q5r4uon05a32vhs29cd65sarv6 

The second cookie, that just "appears" when I make the request 
Cookie: $Version=0; PHPSESSID=hb94i7vopft13uaaob837lf3b0; $Path=/ 

那麼,什麼是這個版本0的cookie,以及如何確保它不會在我的要求通過?

ArrayList<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>(); 
URL page = null; 
Scanner scanner = null; 
Session session = null; 
HttpURLConnection connection = null; 
try { 
    link = link.replace("&amp;", "&"); 
    page = new URL(FS_PARTIAL_SESSION_URL + link); 


    StringBuilder sb = new StringBuilder(); 
    Iterator<Cookie> cooks = cookies.iterator(); 
    while(cooks.hasNext()) { 
     Cookie cookie = cooks.next(); 
     sb.append(cookie.getName()+"="+cookie.getValue()); 
     if (cooks.hasNext()) { 
      sb.append(";"); 
     } 
    } 

    connection = (HttpURLConnection) page.openConnection(); 
    connection.setRequestProperty("Cookie", sb.toString()); 
    scanner = new Scanner(connection.getInputStream()); 

} catch (MalformedURLException e) { 
    log.log(Level.SEVERE, e.getMessage(), e); 
} catch (IOException e) { 
    log.log(Level.SEVERE, e.getMessage(), e); 
} 
finally { 
    connection.disconnect(); 
} 

回答

0

我終於通過創建我自己的「版本0」cookie解決了這個問題。當這樣做的時候,看起來第二個cookie不會被創建,或者至少我自己的cookie被取代。因此,在我的while循環中,代碼現在看起來像這樣:

Cookie cookie = cooks.next(); 
sb.append("$Version=0; "); 
sb.append(cookie.getName()+"="+cookie.getValue()); 
if (cooks.hasNext()) { 
    sb.append(";"); 
} 
sb.append("; $Path=/");