如何避免在 java unirest 请求中发送 Cookie header?
How to avoid sending Cookie header in java unirest requests?
我注意到使用 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();
}
}
这可能是由于底层 HttpClient 实现的默认设置所致。设置自定义 HttpClient 似乎可行:
HttpClient httpClient = HttpClients.custom()
.disableCookieManagement()
.build();
Unirest.setHttpClient(httpClient);
看起来像
Unirest.config().enableCookieManagement(false);
解决问题。
我注意到使用 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();
}
}
这可能是由于底层 HttpClient 实现的默认设置所致。设置自定义 HttpClient 似乎可行:
HttpClient httpClient = HttpClients.custom()
.disableCookieManagement()
.build();
Unirest.setHttpClient(httpClient);
看起来像
Unirest.config().enableCookieManagement(false);
解决问题。