所以這是目前我的應用程序如何設置:接受所有Cookie通過HttpClient
1.)登錄活動。 2.)一旦登錄,其他活動可能會啓動,使用PHP腳本,需要從登錄發送的Cookie。
我在我的應用程序中使用一個HttpClient以確保使用相同的cookie,但我的問題是我得到3個被拒絕的cookies中的2個。我不在乎cookies的有效性,但我確實需要他們接受。 I tried setting the CookiePolicy,但那也沒有奏效。這是logcat的是說:
11-26 10:33:57.613: WARN/ResponseProcessCookies(271): Cookie rejected: "[version: 0] [name: cookie_user_id][value: 1][domain: www.trackallthethings.com][path: trackallthethings][expiry: Sun Nov 25 11:33:00 CST 2012]". Illegal path attribute "trackallthethings". Path of origin: "/mobile-api/login.php"
11-26 10:33:57.593: WARN/ResponseProcessCookies(271): Cookie rejected: "[version: 0][name: cookie_session_id][value: 1985208971][domain: www.trackallthethings.com][path: trackallthethings][expiry: Sun Nov 25 11:33:00 CST 2012]". Illegal path attribute "trackallthethings". Path of origin: "/mobile-api/login.php"
我相信,我的實際代碼是正確的(我的應用程序仍然登錄正確的,只是不接受上述餅乾),但在這裏它是無論如何:
HttpGet httpget = new HttpGet(//MY URL);
HttpResponse response;
response = Main.httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
從這裏我使用StringBuilder來獲取響應的字符串。沒有什麼花哨。
我知道我的cookies被拒絕的原因是因爲「非法路徑屬性」(我在/mobile-api/login.php上運行腳本,而cookie將返回路徑爲「/ 「對於trackallthethings),但我想無論如何接受餅乾。有沒有辦法做到這一點?
這絕對是正確的答案。我以爲我做錯了什麼,但事實證明,我的項目合作伙伴正在用不正確的路徑發放cookie。 :-) – Vinay