如何避免在Java unirest请求中发送Cookie标头?

Zac*_*Zac 3 java cookies unirest

我注意到,使用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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Zac*_*Zac 5

这可能是由于基础HttpClient实现上的默认设置。设置自定义HttpClient似乎可行:

HttpClient httpClient = HttpClients.custom()
    .disableCookieManagement()
    .build();
Unirest.setHttpClient(httpClient);
Run Code Online (Sandbox Code Playgroud)