接受所有Cookie通过HttpClient的 [英] Accept All Cookies via HttpClient

查看:167
本文介绍了接受所有Cookie通过HttpClient的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是目前如何我的应用程序设置:

So this is currently how my app is set up:

1)登录活动。 2)登录后,其他活动也被解雇了,需要从登录发送的cookie的使用PHP脚本。

1.) Login Activity. 2.) Once logged in, other activities may be fired up that use PHP scripts that require the cookies sent from logging in.

我使用一个HttpClient的在我的应用程序,以确保相同的Cookie被使用,但我的问题是,我得到2的3饼干拒绝。我不关心饼干的有效性,但我也需要他们所接受。 <一href="http://stackoverflow.com/questions/874903/how-can-i-force-javas-httpclient-to-accept-invalid-cookies">I尝试设置的CookiePolicy ,但是这并没有任何工作。这是logcat的是说:

I am using one HttpClient across my app to ensure that the same cookies are used, but my problem is that I am getting 2 of the 3 cookies rejected. I do not care about the validity of the cookies, but I do need them to be accepted. I tried setting the CookiePolicy, but that hasn't worked either. This is what logcat is saying:

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"

我相信,我的实际code是正确的(我的应用程序仍然记录在正确的,只是没有接受上述的Cookie),但在这里它是无论如何:

I am sure that my actual code is correct (my app still logs in correctly, just doesn't accept the aforementioned cookies), but here it is anyway:

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简单地获得响应的字符串。没什么特别的。

From here I use the StringBuilder to simply get the String of the response. Nothing fancy.

据我所知,我的饼干被拒绝的原因是因为非法路径属性(我运行在/mobile-api/login.php脚本,而该cookie将只是/的路径返回对于trackallthethings),但我愿意接受饼干,无论如何。有没有办法做到这一点?

I understand that the reason my cookies are being rejected is because of an "Illegal path attribute" (I am running a script at /mobile-api/login.php whereas the cookie will return with a path of just "/" for trackallthethings), but I would like to accept the cookies anyhow. Is there a way to do this?

推荐答案

这是你所面临的问题似乎是由设计,私密性/安全性的目的。一般而言,任何资源不允许设置cookie它将不能够接收。在这里,您要的路径设置cookie trackallthethings 从资源 /mobile-api/login.php 这显然是行不通的。

The issue that you are facing seems to be by design for privacy/security purpose. In general any resource is not allowed to set a cookie it will not be able to receive. Here you are trying to set the cookie with the path trackallthethings from the resource /mobile-api/login.php which obviously is not working.

在这里,你有以下两种选择

Here you have following two options

  1. 设置与可以同时为资源的路径饼干(这可能是根/)或
  2. 定义自定义的Cookie政策并注册自己的cookie支持。这里是相关文档例如
  1. Set the cookie with the path which is accessible to both the resources (this may be root '/') OR
  2. Define a custom cookie policy and Registering your own cookie support. Here is related documentation and example.

希望这有助于。

这篇关于接受所有Cookie通过HttpClient的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆