I need use cookies on an https connection from an android native app.
I am using RestTemplate.
Checking other threads
(eg. Setting Security cookie using RestTemplate)
I was able to handle cookies within an http connection:
restTemplate.setRequestFactory(new YourClientHttpRequestFactory());
where YourClientHttpRequestFactory extends SimpleClientHttpRequestFactory
this works fine on http but not on https.
On the other hand I was able to sort out the https problem of Android trusting the SSL certificate:
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(HttpUtils.getNewHttpClient()));
where HttpUtils is described here:
http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html
My problem is that I need to use a single implementation of ClientHttpRequestFactory.
So I have 3 options:
1) find a way to handle https using SimpleClientHttpRequestFactory
2) find a way to handle cookies using HttpComponentsClientHttpRequestFactory
3) use another approach
I had the same problem. Here is my solution:
First, I handled SSL the same way you did (I used Bob Lee’s method).
Cookies are a different story. The way I’ve handled cookies in the past without RestTemplate (i.e. just using Apache’s HttpClient class directly) is to pass an instance of HttpContext into HttpClient’s execute method. Let’s take a step back…
HttpClient has a number of overloaded execute methods, one of which is:
The instance of HttpContext can have a reference to a CookieStore. When you create an instance of HttpContext, provide a CookieStore (either a new one, or one you saved from a previous request):
Of course, you can add cookies to the instance of CookieStore before sending the request if you like. Now when you call the execute method, use that instance of HttpContext:
(where httpRequester is an instance of HttpPost, HttpGet, etc.)
If you need to resend any cookies on subsequent requests, make sure you store the cookies somewhere:
The StaticCacheHelper class that is used in this code is just a custom class that can store data in a static Map:
BUT!!!! As of 01/2012 The RestTemplate in Spring Android doesn’t give you access to add an HttpContext to the execution of the request!! This is being fixed in Spring Framework 3.1.0.RELEASE and that fix is scheduled to be migrated into Spring Android 1.0.0.RC1.
So, when we get Spring Android 1.0.0.RC1, we should be able to add the context as described in the above example. Until then, we have to add/pull the cookies from the request/response headers using a ClientHttpRequestInterceptor.
The intercepter intercepts the request, looks in the cache to see if there are any cookies to add to the request, then executes the request, then pulls any cookies off the response and stores them for future use.
Add the interceptor to the request template:
And there you go! I’ve tested that and it works. That should hold you over until Spring Android 1.0.0.RC1 when we can use the HttpContext directly with RestTemplate.
Hopes this helps others!!