2017-06-12 211 views

回答

5
final String url = "http://codeflex.co:8080/rest/Management/login"; 

RestTemplate template = new RestTemplate(); 
Credentials cred = new Credentials(); 
cred.setUserName("[email protected]"); 
cred.setPassword("godmode"); 

HttpEntity<Credentials> request = new HttpEntity<>(cred);  
HttpEntity<String> response = template.exchange(url, HttpMethod.POST, request, String.class); 
HttpHeaders headers = response.getHeaders(); 
String set_cookie = headers.getFirst(HttpHeaders.SET_COOKIE); 

代碼從the example

1
HttpHeaders requestHeaders = new HttpHeaders(); 
requestHeaders.add("Cookie", "JSESSIONID=" + session.getValue()); 
HttpEntity requestEntity = new HttpEntity(null, requestHeaders); 
ResponseEntity rssResponse = restTemplate.exchange(
    "https://jira.example.com/sr/jira.issueviews:searchrequest-xml/18107/SearchRequest-18107.xml?tempMax=1000", 
    HttpMethod.GET, 
    requestEntity, 
    Rss.class); 
Rss rss = rssResponse.getBody(); 

http://springinpractice.com/2012/04/08/sending-cookies-with-resttemplate