java – 通过HttpClient接受所有Cookie

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

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

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

我确信我的实际代码是正确的(我的应用程序仍然正确登录,只是不接受上述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();

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

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

解决方法:

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

这里有以下两个选项

>使用两个资源都可访问的路径设置cookie(这可能是root’/’)OR
>定义自定义cookie策略并注册自己的cookie支持.这是related documentationexample.

希望这可以帮助.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...