I need to make two requests to the server. In the first request I send parameter userID = 1 and the server returns a cookie _session_ID. Now I send a second request with no parameters and the result should be the same as the first time. But the cookie isn't sent. Why?
This is my code:
public class Server
{
static HttpClient httpclient;
static HttpPost httppost;
static String JsonString;
static HttpResponse response;
static List <NameValuePair> nameValuePairs;
static CookieStore cookieStore;
static HttpContext localContext;
static List<Cookie> cookies;
static int cookieSize;
public static void Clear()
{
try
{
cookieStore = new BasicCookieStore();
localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://mobile-app-storage.herokuapp.com/test");
nameValuePairs = new ArrayList <NameValuePair> (2);
nameValuePairs.add(new BasicNameValuePair("userID", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost, localContext);
JsonString = ReadFromServer(response.getEntity().getContent());
Log.e("Request", "1");
cookies = cookieStore.getCookies();
cookieSize = cookies.size();
for (int i = 0; i < cookieSize; i++)
{
Log.v("Cookie "+ i, "name: "+cookies.get(i).toString());
}
// Second request without userID. Must be same result as first
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://mobile-app-storage.herokuapp.com/test");
nameValuePairs.clear();
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
JsonString = ReadFromServer(response.getEntity().getContent());
Log.e("Request", "2");
cookies = cookieStore.getCookies();
cookieSize = cookies.size();
for (int i = 0; i < cookieSize; i++)
{
Log.v("Cookie "+ i, "name: "+cookies.get(i).toString());
}
}
catch (Throwable e) {Log_.Error(e);}
}
}