My app does HTTP POSTS, in general the app works, but I am having trouble with the cookies. My code seems to get the cookies fine, through the HttpClient variable and out, but the results of the POST reveal that my app (as a client), is not remembering anything. (When I use a webbroswer in my pc, it works fine, the cookies remember my name) Also, if the app is restarted, the cookies disappear (...but first things first)
This is my code:
static DefaultHttpClient httpclient;
static HttpPost httppost;
static CookieStore cookiestore;
new Thread(new Runnable() {
public void run() {
String URI_FOR_DOMAIN="http://chatbot.t.com/cgi-bin/elbot.cgi";
httppost = new HttpPost(URI_FOR_DOMAIN);
try {
HttpResponse resp = httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
cookiestore = ((DefaultHttpClient) httpclient).getCookieStore();
List<Cookie> list = cookiestore.getCookies();
app.logy("COOKIE STORE 1: " + cookiestore.toString());
app.logy("COOKIE LIST 1: "+list.toString());
for (int i=0;i<list.size();i++) {
Cookie cookie = list.get(i);
app.logy("COOKIE "+i+": "+cookie.toString());
}
HttpContext context = new BasicHttpContext();
context.setAttribute( ClientContext.COOKIE_STORE, cookiestore);
httppost = new HttpPost(url);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<>(2);
nameValuePairs.add(new BasicNameValuePair("ENTRY", userInput));
nameValuePairs.add(new BasicNameValuePair("IDENT", userIDENT));
nameValuePairs.add(new BasicNameValuePair("USERLOGID", userLOGID));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
app.logy("COOKIE STORE 2: "+cookiestore.toString());
app.logy("COOKIE CONTEXT: " + context.toString());
HttpResponse response = httpclient.execute(httppost, context);
cookiestore = ((DefaultHttpClient) httpclient).getCookieStore();
List<Cookie> list2 = cookiestore.getCookies();
app.logy("COOKIE STORE 2: " + cookiestore.toString());
app.logy("COOKIE LIST 2: "+list2.toString());
for (int i=0;i<list2.size();i++) {
Cookie cookie = list2.get(i);
app.logy("COOKIE2 i="+i+": "+cookie.toString());
}
HttpEntity entity = response.getEntity();
}
}
app.logy(sb.toString());
}
catch (IOException e) { e.printStackTrace(); }
catch (Exception e) { e.printStackTrace(); }
} catch (ClientProtocolException e) {
app.logy("HttpPOST (ClientProtocol) error: "+e);
} catch (IOException e) {
app.logy("HttpPOST (I/O) error: "+e);
}
}
}).start();
This are the values that are reflected in the Logcats from the above code. The cookies seem to be there, but HttpResponse response = httpclient.execute(httppost, context); seems to ignore them.
COOKIE STORE 2: [[version: 0][name: cookie_user_name][value: Albert][domain: elbot-e.artificial-solutions.com][path: /][expiry: Thu Oct 08 22:26:54 GMT+02:00 2015]]
COOKIE LIST 2: [[version: 0][name: cookie_user_name][value: Albert][domain: elbot-e.artificial-solutions.com][path: /][expiry: Thu Oct 08 22:26:54 GMT+02:00 2015]]
COOKIE2 i=0: [version: 0][name: cookie_user_name][value: Albert][domain: elbot-e.artificial-solutions.com][path: /][expiry: Thu Oct 08 22:26:54 GMT+02:00 2015]