我需要从CloseableHttpClient迁移到我的桌面客户端应用程序中的RestTemplate。RestTemplate响应有一个空的"set -cookie";头。
源代码(正常工作)://返回Set-Cookie报头= "JSESSIONID=D2442…">
List<BasicNameValuePair> parameters = asList(
new BasicNameValuePair("username", username),
new BasicNameValuePair("password", password));
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(parameters));
CloseableHttpClient client = createDefault()
CloseableHttpResponse response = client.execute(httpPost);
目标代码://返回Set-Cookie报头= ">
String url = url;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("username", username);
map.add("password", password);
ParameterizedTypeReference<Map<String, String>> responseType = new ParameterizedTypeReference<>() {};
HttpEntity<Object> request = new HttpEntity<>(map, headers);
ResponseEntity<Map<String, String>> tokensInfo = restTemplate.exchange(
url, HttpMethod.POST, request, responseType
);
请帮助。
添加到目标代码:
HttpClient httpClient = HttpClientBuilder.create().build();
ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
restTemplate.setRequestFactory(factory);