【发布时间】:2015-12-03 05:23:50
【问题描述】:
我注意到使用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();
}
}
【问题讨论】:
-
恕我直言,遗憾的是:1)两个视觉上独立的代码段通过静态保存的状态连接; 2) 默认情况下启用此类行为(cookie 存储)。这对于开发人员来说既出乎意料又不安全,因为共享状态会导致应用程序的可预测行为。 Unirest 的配置也通过静态设置器进行管理。这就是我停止使用 Unirest 的原因。