HttpClient如何检索被拒绝的cookie

时间:2018-04-19 13:38:19

标签: java httpclient resteasy

我正在使用resteasy客户端3.0.6和httpclient 4.5.2,并且在执行GET请求时收到以下警告:

Apr 19, 2018 12:40:18 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Cookie rejected [MY_COOKIE_IDENTITY="eyJhbGciOiJIUzI1NiIsImtpZCIgOiAiNzE5OTk3ZGQtNjVhZi00OTMwLTgwMjYtYWU3ZjIxZDYyYWI3In0.eyJqdGkiOiIyZjdh...", version:1, domain:psp208.fx.lan, path:/auth/210, expiry:null] Illegal 'path' attribute "/auth/210". Path of origin: "/auth/001"
Apr 19, 2018 12:40:18 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Cookie rejected [MY_COOKIE_SESSION="epos210/770a1a6f-1901-4985-8330-5aab4f9febe8/5f45c340-dfb6-466a-8f96-9d5a3cc51643", version:0, domain:psp208.fx.lan, path:/auth/210, expiry:Thu Apr 19 22:40:18 CEST 2018] Illegal 'path' attribute "/auth/210". Path of origin: "/auth/001"

但即使它们被定义为非法路径,我也想以编程方式检索它们。目前我正在尝试使用以下代码段:

ResteasyClient client = new ResteasyClientBuilder().connectionPoolSize(20).httpEngine(apacheHttpClient4Engine).build();
client.register(new ClientRequestFilter(){

    @Override
    public void filter(ClientRequestContext arg0) throws IOException {
        cookies.putAll(arg0.getCookies());
    }

});

但永远不会调用过滤器。

在查看HttpClient文档后,我尝试创建自己的CookieSpecProvider:

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

        @Override
        public CookieSpec create(HttpContext arg0) {
            return new BrowserCompatSpec(){

                @Override
                public void validate(org.apache.http.cookie.Cookie cookie, CookieOrigin origin)
                        throws MalformedCookieException {

                    // empty implementation
                }


            };
        }

    })
.build();

通过使用此实现,调用ClientRequestFilter但它返回一个空的cookie映射。

我的目标是检索从响应中获得的两个cookie。我该怎么办?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我通过添加cookie商店解决了

    BasicCookieStore cookieStore = new BasicCookieStore();
    CloseableHttpClient closableclient = HttpClients.custom()
            .setDefaultRequestConfig(globalConfig)      
            .setDefaultCookieSpecRegistry(registry)
            .setDefaultCookieStore(cookieStore)
            .build();

通过这种方式,我可以在没有ClientRequestFilter的情况下检索cookie。