I have been trying to use the same HttpClient instance throughout different activities, but it was not working. So I decided to just try one activity and use HttpClient to log in, and then log out once the log in was successful. This is all in the same activity, so HttpClient should definitely keep the session correct?
HttpClient client = new DefaultHttpClient();
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair(UN_ID, username));
postParameters.add(new BasicNameValuePair(PW_ID, password));
HttpPost request = new HttpPost(URL);
formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
response = client.execute(request);
If the response is a successful log in, I try to log out:
if (responseCode.equals("101")){ //successful
HttpGet g = new HttpGet("https://site.com/file.php?logout=1");
HttpResponse r = client.execute(g);
This returns a “not logged in” response. Is this a problem with the server, HttpClient or my code?
EDIT: Response to Eric’s answer.
I have already tried this with cookies:
CookieStore cookieStore = new BasicCookieStore();
httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
Then changed each response from above to include the context:
response = client.execute(request, httpContext);
HttpResponse r = client.execute(g, httpContext);
This is because cookies are not activated by default. You need to use HttpContext as described on this question: How do I manage cookies with HttpClient in Android and/or Java?
EDIT AFTER DISCUSSION (see comments)
Looks like the error came from a problem with the server implementation of the authentication process