我有一个问题,我认为它可能与 HttpClient 有关。我正在登录一个网站并使用 JSoup 从中获取数据。一切正常。这是我的问题,当我想登录不同的帐户时,它会显示来自其他帐户的相同数据。只有当我终止应用程序时,我才能使用不同的凭据登录。我认为我与该网站的 session 仍存储在同一个 HttpClient 中,除非我注销,否则不会让我使用不同的帐户再次登录。解决此问题的最佳方法是什么?使用 HttpGet 方法执行注销脚本?或者有没有办法重置 HttpCLient。谢谢。代码:
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();
}
最佳答案
你可以调用httppost.abort();
关于java - 销毁或关闭 HttpClient Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8496191/