通过HttpClient接受所有Cookie

Vin*_*nay 2 java cookies android httpclient

所以这是我的应用程序的设置方式:

1.)登录活动.2.)登录后,可能会启动其他活动,这些活动使用需要登录时发送的cookie的PHP脚本.

我在我的应用程序中使用一个HttpClient来确保使用相同的cookie,但我的问题是我收到了3个cookie中的2个被拒绝.我不关心cookie的有效性,但我确实需要它们被接受.我尝试设置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"
Run Code Online (Sandbox Code Playgroud)

我确信我的实际代码是正确的(我的应用程序仍然正确登录,只是不接受上述cookie),但无论如何它在这里:

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();
Run Code Online (Sandbox Code Playgroud)

从这里我使用StringBuilder来简单地获取响应的String.没有什么花哨.

我理解我的cookie被拒绝的原因是因为"非法路径属性"(我在/mobile-api/login.php上运行脚本,而cookie将返回路径为"/",用于trackallthethings) ,但无论如何我想接受饼干.有没有办法做到这一点?

San*_*osh 7

您面临的问题似乎是出于隐私/安全目的而设计的.通常,不允许任何资源设置它将无法接收的cookie.在这里,您尝试使用显然不起作用trackallthethings的资源路径设置cookie /mobile-api/login.php.

这里有以下两个选项

  1. 使用两个资源都可访问的路径设置cookie(这可能是root '/')或
  2. 定义自定义cookie策略并注册您自己的cookie支持.这是相关的文档示例.

希望这可以帮助.