1

I have a problem that I think it may relate to the HttpClient. I am logging into a website and grabbing data from it using JSoup. Everything works fine. Here is my issue, when I want to log into a different account, it displays the same data from the other account. Only when I kill the app is when I am able to login with different credentials. I think that my session with the website is still stored in the same HttpClient and won't let me log in again with a different account unless I logout. What would be the best way to fix this problem? Using HttpGet method to execute the logout script? Or is there a way to reset the HttpCLient. Thanks. Code:

public void parseDoc() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            final HttpParams params = new BasicHttpParams();
            HttpClientParams.setRedirecting(params, true);
            httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(
                    "https://secure.groupfusion.net/processlogin.php");
            String HTML = "";
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                        3);
                nameValuePairs
                        .add(new BasicNameValuePair(
                                "referral_page",
                                "/modules/gradebook/ui/gradebook.phtml?type=student_view&jli=t&jli=t&jli=t&jli=t&jli=t&jli=t&printable=FALSE&portrait_or_landscape=portrait"));
                nameValuePairs.add(new BasicNameValuePair("currDomain",
                        "beardenhs.knoxschools.org"));
                nameValuePairs.add(new BasicNameValuePair("username",
                        username.getText().toString()));
                nameValuePairs.add(new BasicNameValuePair("password",
                        password.getText().toString()));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpclient.execute(httppost);

                HTML = EntityUtils.toString(response.getEntity());
                Document doc = Jsoup.parse(HTML);
                Element link = doc.select("a").first();
                String linkHref = link.attr("href");
                HttpGet request = new HttpGet();
                try {
                    request.setURI(new URI(linkHref));

                } catch (URISyntaxException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                response = httpclient.execute(request);

                InputStream in = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(in));
                StringBuilder str = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    str.append(line);
                }
                in.close();
                HTML = str.toString();
                doc = Jsoup.parse(HTML);
                Elements divs = doc.getElementsByTag("tbody");
                for (Element d : divs) {
                    if (i == 2) {
                        finishGrades();
                        break;
                    }
                    i++;
                    ggg = d.html();

                }

            } catch (ClientProtocolException e) {
            } catch (IOException e) {
            }

        }
    }).start();

}
Eli
  • 262
  • 5
  • 15
  • The app is still in the memory thats why only when the app is killed you are getting new data for the new login. Create ability to remove credentials or store credentials. Every run you have new instances of the HttpClient that would not be problem. – Nikola Despotoski Dec 13 '11 at 21:17
  • You do have the ability to store credentials using the "Remember me?" I added to the app. I don't want the user to have to kill the app for somebody else to log in with their credentials. Anyway around this? Thanks. – Eli Dec 13 '11 at 21:21
  • Make sure the credentials are erased from the preferences or anywhere you store them. Investigate if this is not causing your issue. Suggestion: Overwrite them everytime new user is loging then proceed with the login. – Nikola Despotoski Dec 13 '11 at 21:27
  • Thanks for help, but I am certain it as nothing to do with the saved credentials. – Eli Dec 13 '11 at 21:29
  • As @Nikola mentioned, have you tried to create a new `HttpClient` each time? There may be internal storage of cookies or other auth values. Possibly related to: http://stackoverflow.com/questions/4146861/android-httpclient-persistant-cookies – Gray Dec 13 '11 at 22:27
  • In my code I believe I do that, wouldnt `httpclient = new DefaultHttpClient();` do the trick? This is ran every time they are logged in to ensure a new HttpClient. Am I right? Thanks. – Eli Dec 13 '11 at 22:35
  • Are you using anything that needs to invalidated after the data is supplied? This code cannot be cause of the problem. Since every run you have new instance of fewer of the objects, as far as I can see. Make sure all references get the NEW data they suppose to. – Nikola Despotoski Dec 13 '11 at 23:06
  • I am now positive that the second time I try and log in, its rejecting it. This is of course without killing the app. What could the source of this be? Also, it's not because I am still logged in. It's just returning blank html every time I try to log in **the second time**, even if they are invalid credentials. – Eli Dec 13 '11 at 23:36

3 Answers3

4

You can call httppost.abort();

Kevin Parker
  • 16,975
  • 20
  • 76
  • 105
  • Thanks for the reply, but still no go. This is really weird, and I cannot figure out where the problem is even exactly at. I know that the HttpClient is keeping my session with the website, or else I would not even be able to login and get to certain pages. Thanks – Eli Dec 13 '11 at 22:09
1

I ran into a similar problem, although I am using one instance of the HttpClient for the whole application (Singleton). So in that case, if you use preemptive authentication, you can simply do this for the application-wide client instance (e.g. if the user logs out):

public void logOut() {
  // keep in mind, 'httpClient' is final and set once in the constructor (Singleton)
  httpClient.getCredentialsProvider().clear();
  httpClient.getCookieStore().clear(); // important
}

See also: HttpClient State Management

Blacklight
  • 3,809
  • 2
  • 33
  • 39
0

I would suggest setting "no-cache" header of httpclient to false and try. Not guaranteed solution.

kosa
  • 65,990
  • 13
  • 130
  • 167