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

2024-02-16

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

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

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

HttpClient httpClient = HttpClients.custom()
    .disableCookieManagement()
    .build();
Unirest.setHttpClient(httpClient);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何避免在java unirest请求中发送Cookie头? 的相关文章

随机推荐