我必须使用带有Spring Boot的Java通过REST接口实现将文档上载到归档系统。
有人告诉我先用基本身份验证发出GET请求。这将在响应中给我授权cookie。然后,我必须将这些cookie与POST请求一起发送,以进行实际的归档。
GET工作正常。 我在互联网上阅读到,应该在响应的标题“ Set-Cookie”中获取Cookie。 但是我没有饼干。
奇怪的是,如果我用邮递员执行请求,则会得到2个cookie(“ AuthSessionId”和“ ClientId”)。 为什么我无法通过编程方式获得这些信息?
作为旁注:邮递员还显示我在响应中得到了15个(其他?)标题。我在ClientHttpResponse
中找到它们没有问题以下是一些代码:
ClientHttpResponse response = request.execute();
// this is org.springframework.http.client.ClientHttpResponse
List<String> cookies = response.getHeaders().get(HttpHeaders.SET_COOKIE);
if (cookies != null) {
for (String cook : cookies) {
System.out.println("cookie: " + cook);
}
} else {
System.out.println("no cookie in " + HttpHeaders.SET_COOKIE); // this is what I get
}
答案 0 :(得分:0)
kumesana是正确的。 Cookie被过滤掉并放入CookieStore。如果我之前进行过设置,则可以在请求完成后获取它们:
CookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient client = HttpClientBuilder
.create()
.setDefaultCredentialsProvider(credentialsProvider)
.setConnectionManager(poolingConnectionManager)
.setDefaultCookieStore(cookieStore)
.build();