简体   繁体   English

通过HttpClient接受所有Cookie

[英]Accept All Cookies via HttpClient

So this is currently how my app is set up: 因此,这是我的应用当前的设置方式:

1.) Login Activity. 1.)登录活动。 2.) Once logged in, other activities may be fired up that use PHP scripts that require the cookies sent from logging in. 2.)登录后,可能会触发其他使用PHP脚本的活动,这些活动需要从登录发送的cookie。

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. 我在整个应用程序中使用一个HttpClient来确保使用相同的cookie,但是我的问题是3个cookie中有2个被拒绝了。 I do not care about the validity of the cookies, but I do need them to be accepted. 我不在乎cookie的有效性,但是我确实需要接受它们。 I tried setting the CookiePolicy , but that hasn't worked either. 我尝试设置CookiePolicy ,但这也没有用。 This is what logcat is saying: 这就是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"

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

From here I use the StringBuilder to simply get the String of the response. 从这里,我使用StringBuilder来简单地获取响应的字符串。 Nothing fancy. 没有什么花哨。

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. 我了解我的Cookie被拒绝的原因是由于“路径属性非法”(我在/mobile-api/login.php中运行脚本,而cookie会以trackallthethings的路径返回“ /”) ,但无论如何我都想接受Cookie。 Is there a way to do this? 有没有办法做到这一点?

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. 通常,不允许任何资源设置将无法接收的cookie。 Here you are trying to set the cookie with the path trackallthethings from the resource /mobile-api/login.php which obviously is not working. 在这里,您尝试使用资源/mobile-api/login.php的路径trackallthethings设置cookie,这显然不起作用。

Here you have following two options 在这里,您有以下两种选择

  1. Set the cookie with the path which is accessible to both the resources (this may be root '/' ) OR 使用两个资源都可以访问的路径设置cookie(这可能是根'/' ),或者
  2. Define a custom cookie policy and Registering your own cookie support. 定义自定义cookie策略并注册您自己的cookie支持。 Here is related documentation and example . 这是相关的文档示例

Hope this helps. 希望这可以帮助。

Since the API of HttpClient seems to change very fast, here is some working example code for HttpClient 4.5.1 to allow all (malformed) cookies: 由于HttpClient的API似乎变化非常快,因此以下是HttpClient 4.5.1一些有效示例代码,以允许所有(格式错误)cookie:

class EasyCookieSpec extends DefaultCookieSpec {
    @Override
    public void validate(Cookie arg0, CookieOrigin arg1) throws MalformedCookieException {
        //allow all cookies 
    }
}

class EasySpecProvider implements CookieSpecProvider {
    @Override
    public CookieSpec create(HttpContext context) {
        return new EasyCookieSpec();
    }
}

Registry<CookieSpecProvider> r = RegistryBuilder.<CookieSpecProvider>create()
            .register("easy", new EasySpecProvider())
            .build();

CookieStore cookieStore = new BasicCookieStore();

RequestConfig requestConfig = RequestConfig.custom()
            .setCookieSpec("easy")
            .build();

CloseableHttpClient httpclient = HttpClients.custom()
            .setDefaultCookieStore(cookieStore)
            .setDefaultCookieSpecRegistry(r)
            .setDefaultRequestConfig(requestConfig)
            .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM