Reputation: 262
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();
}
Upvotes: 1
Views: 4794
Reputation: 3827
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
Upvotes: 1
Reputation: 66657
I would suggest setting "no-cache" header of httpclient to false and try. Not guaranteed solution.
Upvotes: 0