Java 通过 HttpClient 接受所有 Cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8279970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-15 00:37:16  来源:igfitidea点击:

Accept All Cookies via HttpClient

javaandroidcookieshttpclient

提问by Vinay

So this is currently how my app is set up:

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

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.

1.) 登录活动。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. 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:

我在我的应用程序中使用一个 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"

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. Nothing fancy.

从这里我使用 StringBuilder 来简单地获取响应的字符串。没有什么花哨。

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 被拒绝的原因是因为“非法路径属性”(我在 /mobile-api/login.php 运行一个脚本,而 cookie 将返回一个路径为“/”的 trackallthethings) ,但无论如何我都愿意接受cookies。有没有办法做到这一点?

采纳答案by Santosh

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 trackallthethingsfrom the resource /mobile-api/login.phpwhich obviously is not working.

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

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
  2. Define a custom cookie policy and Registering your own cookie support. Here is related documentationand example.
  1. 使用两个资源都可以访问的路径设置 cookie(这可能是 root '/')或
  2. 定义自定义 cookie 策略并注册您自己的 cookie 支持。这是相关的文档示例

Hope this helps.

希望这可以帮助。

回答by Johannes Kronmüller

Since the API of HttpClientseems to change very fast, here is some working example code for HttpClient 4.5.1to allow all (malformed) cookies:

由于 的 APIHttpClient似乎变化非常快,这里有一些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();