Java进行http请求时,放置会话信息到header里面
public class CreateHttpTest {
public static void main(String[] args) {
createHttp();
}
public static void createHttp() {
RestTemplate template = new RestTemplate();
ClientHttpRequestFactory clientFactory = new HttpComponentsClientHttpRequestFactory();
template.setRequestFactory(clientFactory);
String urlStr = "http://ip:port/app/api/depart/getUsers";
URI uri = UriComponentsBuilder.fromUriString(urlStr)
//.queryParam("sid", "42581a0dc0294e97ac8e3331875cfaaa")
.build().encode().toUri();
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Cookie", "sid=4c86eebfae6e4eef9e22b7caa6a4b684");
requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON_UTF8));
requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> requestEntity = new HttpEntity<String>("",requestHeaders);
ResponseEntity<String> responseEntity = template.exchange(uri, HttpMethod.GET,
requestEntity,String.class);
System.out.println("返回信息" + responseEntity.getBody());
}
}