0

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);
Ryan
  • 1,135
  • 1
  • 13
  • 28
  • Does your server responds to http mode instead of https, so you could rule out a problem coming from the https management? Also, did you successfully simulated a session using for instance curl, so we are sure the server is working ok? – EricLarch May 08 '12 at 15:37
  • Http did not help. I have been using Fiddler to create my own http requests. I can log in this way, but I cannot do anything after that, because I get the "not logged in" response. The URL I am going to is for the mobile version. I can use Fiddler to create requests on the main(not mobile) site and it all works fine. I can navigate to pages that are only available when logged in. This is why I think the php on the mobile website is not keeping the session correctly. I don't have access to the php, but I can ask someone who does, if you think this could be the problem. – Ryan May 08 '12 at 15:44
  • Then just try using the non mobile URL, you'll see if your Android code is correct. If you can access private pages through your code using the main URL then you'll know for sure the problem comes from the mobile version. – EricLarch May 08 '12 at 15:49
  • That was a good idea. The response from the main site is just the page source, but I think my code got back the correct source. I am having the PHP looked at, he thinks he may not be starting a session in one of the php files. I hope that is the problem. Thanks for your time. – Ryan May 08 '12 at 16:25

1 Answers1

0

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

Community
  • 1
  • 1
EricLarch
  • 5,653
  • 5
  • 31
  • 37