Jersey ClientResponse getCookies无法正确解析

时间:2015-12-16 03:45:46

标签: java cookies jersey-client

我有一些Java代码正在调用一个宁静的服务,并且需要首先验证并存储cookie以供将来调用。我正在使用Jersey 1.8。问题是在进行身份验证调用后,我尝试使用ClientResponse.getCookies()检索cookie并遍历NewCookie对象列表。但是,在调用NewCookie.getName()时,将返回cookie的路径。

响应标头中返回的cookie如下所示:

Set-Cookie:xsIdEEDB0766347B60DAEAA0AF57226EDD2C = 385DF57B79FE0A4D84E04ED43000A81B;路径= /;仅Http

我的代码看起来像这样。我现在只是将信息转储到System.out,因为我想查看调试器中的值。

我的问题是为什么ClientResponse.getCookies()似乎不起作用。手动解析标题将起作用,但这似乎不正确。有没有办法配置客户端或ClientResponse以正确获取cookie?

ClientConfig clientConfig = new DefaultClientConfig();
Client client = Client.create(clientConfig);

Builder builder = client.resource(MY_PATH).accept("text/plain");
builder = builder.header(AUTHORIZATION, auth);

ClientResponse response = builder.head();

if (response.getClientResponseStatus() == Status.OK) {

    // Look at cookies in the response
    List<NewCookie> cs = response.getCookies();
    for (NewCookie c : cs) {
        System.out.println(c.getName());
        System.out.println(c.getValue());
    }

    // Look at the cookies in the header
    List<String> cookies  = headers.get("Set-Cookie");
    for (String cookie : cookies) {
        // example: xsIdEEDB0766347B60DAEAA0AF57226EDD2C=385DF57B79FE0A4D84E04ED43000A81B; path=/; HttpOnly
        int x = cookie.indexOf(';');
        String cookieNameValue = cookie.substring(0, x); 
        String[] nameValue = cookieNameValue.split("=");
        NewCookie nc = new NewCookie(nameValue[0], nameValue[1]);

    }
}

0 个答案:

没有答案