【发布时间】:2011-12-13 21:09:11
【问题描述】:
我有一个问题,我认为它可能与 HttpClient 相关。我正在登录一个网站并使用 JSoup 从中获取数据。一切正常。这是我的问题,当我想登录另一个帐户时,它会显示来自另一个帐户的相同数据。只有当我杀死应用程序时,我才能使用不同的凭据登录。我认为我与网站的会话仍存储在同一个 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();
}
【问题讨论】:
-
应用程序仍在内存中,这就是为什么只有当应用程序被杀死时,您才会为新登录获取新数据。创建删除凭据或存储凭据的能力。每次运行都会有新的 HttpClient 实例,这不会有问题。
-
您确实可以使用“还记得我吗?”来存储凭据。我添加到应用程序。我不希望用户必须杀死该应用程序才能让其他人使用他们的凭据登录。无论如何围绕这个?谢谢。
-
确保凭据已从首选项或存储它们的任何位置删除。调查这是否不是导致您的问题的原因。建议:每次新用户登录时覆盖它们,然后继续登录。
-
感谢您的帮助,但我确信这与保存的凭据无关。
-
正如@Nikola 提到的,您是否尝试过每次都创建一个新的
HttpClient?可能存在 cookie 或其他身份验证值的内部存储。可能与:stackoverflow.com/questions/4146861/…
标签: java android httpclient http-get