How to avoid sending Cookie header in java unirest requests?

↘锁芯ラ 提交于 2019-12-23 17:43:22

问题


I noticed that using unirest java library cookies are by default sent in requests after being set in responses (just like any browser does). Is there any way to avoid it?

Example:

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();
    }
}

回答1:


It is probably due to a default setting on the underlying HttpClient implementation. Setting a custom HttpClient seems to work:

HttpClient httpClient = HttpClients.custom()
    .disableCookieManagement()
    .build();
Unirest.setHttpClient(httpClient);



回答2:


Looks like

Unirest.config().enableCookieManagement(false);

solves the problem.



来源:https://stackoverflow.com/questions/32442318/how-to-avoid-sending-cookie-header-in-java-unirest-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!