本文介绍了如何避免在Java unirest请求中发送Cookie标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,默认情况下,使用 Java库cookie是在请求中发送的响应(就像其他浏览器一样)。有什么方法可以避免呢?

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?

示例:

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似乎可行:

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

这篇关于如何避免在Java unirest请求中发送Cookie标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:49