4

我注意到使用unirest java 库 cookie 默认情况下在响应中设置后在请求中发送(就像任何浏览器一样)。有什么办法可以避免吗?

例子:

public class Main {
    private static HttpResponse<JsonNode> doRequest() throws UnirestException {
        try {
            HttpResponse<JsonNode> jsonResponse = Unirest
                    .get("http://example.com")
                    .header("Accept", "application/json").asJson();
            return jsonResponse;
        } catch (UnirestException e) {
            throw e;
        }

    }
    public static void main(String[] args) throws UnirestException {
        //first request receive a set-cookie header in response
        doRequest();
        //second request send a Cookie header with the cookie set by the first one: can I avoid this?
        doRequest();
    }
}
4

2 回答 2

6

这可能是由于底层 HttpClient 实现的默认设置。设置自定义 HttpClient 似乎有效:

HttpClient httpClient = HttpClients.custom()
    .disableCookieManagement()
    .build();
Unirest.setHttpClient(httpClient);
于 2015-09-11T07:14:22.560 回答
1

好像

Unirest.config().enableCookieManagement(false);

解决问题。

于 2019-03-17T19:05:50.360 回答