2012-04-16 21 views
9

ben RestTemplate kullanıyorum, bir HTTP çerez göndermeye gerek:RestTemplate ve Çerez

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); 

Ancak alıcı sunucusu çerezi görmez.

+0

ve nerede serverside kodudur? - Gerçekten sunucuya neyin gönderildiğini doğrulamak için bir araç (wireshark gibi) kullandınız mı? – Ralph

+1

Hangi 'ClientHttpRequestFactory'' RestTemplate'ınızı oluşturdu? Fabrikaya bağlı olarak, isteğinize otomatik olarak eklenecek çerezleri ekleyebileceğiniz bir çerez deposu olabilir. Bu, ayarlı başlığınızı geçersiz kılabilir. – yincrash

cevap

20

Varsayılan dinlenme şablonu kalıcı bir bağlantı kullanmıyor, burada kullandığım şey.

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; 
    } 
} 
Ayrıca RestTemplate uzatabilirsiniz
+0

Benim için çalışıyor! Doğru cevap olarak işaretle – user48956

6

:

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

Bu, orijinal koddan nasıl farklı? Buradaki anahtarın, en çok kullanılan cevapta belirtildiği gibi, normal RestTemplate'i kullanırken HTTP bağlantısının kalıcı olmayan doğası olduğunu düşünüyorum. –

+0

Orijinal kod, ayarlamanız gereken çerezi ayarlamamıştı ... çünkü çerezi her isteğiyle tanımlamanız gerekir. – ticktock

+1

yeterince adil. Downvote'umu değiştirecek. Açıklama için teşekkürler. –