2012-04-16 156 views
9

我需要發送一個HTTP cookie,我使用RestTemplateRestTemplate和餅乾

HttpHeaders requestHeaders = new HttpHeaders(); 
requestHeaders.add("Cookie", "SERVERID=c52"); 
HttpEntity requestEntity = new HttpEntity(null, requestHeaders); 
ResponseEntity responses = restTemplate.exchange(webService.getValidateUserUrl(), 
     HttpMethod.POST, requestEntity, String.class, mapValidateUser); 

然而,接收服務器不會看到餅乾。

+0

哪裏是服務器端的代碼? - 您是否使用過工具(如wireshark)來驗證發送給服務器的內容? – Ralph 2012-04-17 11:08:22

+1

哪個'ClientHttpRequestFactory'生成你的'RestTemplate'?根據工廠的不同,可能會有一個cookie存儲,您可以將cookie添加到將自動添加到您的請求中的cookie中。這可能會覆蓋您的設置標題。 – yincrash 2012-04-22 23:02:36

回答

20

默認的休息模板不使用持續連接,這裏是我使用的。

public class StatefullRestTemplate extends RestTemplate 
{ 
    private final HttpClient httpClient; 
    private final CookieStore cookieStore; 
    private final HttpContext httpContext; 
    private final StatefullHttpComponentsClientHttpRequestFactory statefullHttpComponentsClientHttpRequestFactory; 

    public StatefullRestTemplate() 
    { 
     super(); 
     HttpParams params = new BasicHttpParams(); 
     HttpClientParams.setRedirecting(params, false); 

     httpClient = new DefaultHttpClient(params); 
     cookieStore = new BasicCookieStore(); 
     httpContext = new BasicHttpContext(); 
     httpContext.setAttribute(ClientContext.COOKIE_STORE, getCookieStore()); 
     statefullHttpComponentsClientHttpRequestFactory = new StatefullHttpComponentsClientHttpRequestFactory(httpClient, httpContext); 
     super.setRequestFactory(statefullHttpComponentsClientHttpRequestFactory); 
    } 

    public HttpClient getHttpClient() 
    { 
     return httpClient; 
    } 

    public CookieStore getCookieStore() 
    { 
     return cookieStore; 
    } 

    public HttpContext getHttpContext() 
    { 
     return httpContext; 
    } 

    public StatefullHttpComponentsClientHttpRequestFactory getStatefulHttpClientRequestFactory() 
    { 
     return statefullHttpComponentsClientHttpRequestFactory; 
    } 

} 


public class StatefullHttpComponentsClientHttpRequestFactory extends HttpComponentsClientHttpRequestFactory 
{ 
    private final HttpContext httpContext; 

    public StatefullHttpComponentsClientHttpRequestFactory(HttpClient httpClient, HttpContext httpContext) 
    { 
     super(httpClient); 
     this.httpContext = httpContext; 
    } 

    @Override 
    protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) 
    { 
     return this.httpContext; 
    } 
} 
+0

適合我!標記爲正確答案? – user48956 2014-01-21 15:45:46

6

您還可以延長RestTemplate:

public class CookieRestTemplate extends RestTemplate { 

    @Override 
    protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException { 
    ClientHttpRequest request = super.createRequest(url, method); 

    request.getHeaders().add("Cookie", "SERVERID=c52"); 
    return request; 
    } 

}

+0

這與原始代碼有何不同?我認爲這裏的關鍵是,正如在最受投票的答案中所述,當使用常規RestTemplate時,HTTP連接的非持久性特性。 – 2016-03-15 17:58:00

+0

原始代碼沒有設置你需要設置的cookie ...因爲你必須定義每個請求的cookie ..這裏你去。 – ticktock 2016-03-15 20:47:45

+1

夠公平的。會改變我的失望。感謝您的解釋。 – 2016-03-15 20:49:40