我有一个登录表单,当前正在使用HTTP Post Request登录参数并登录网站。我不确定服务器类型,所以可能是问题。一旦获取登录凭据,它就会将输入流转换为字符串(所有html)并将其设置为textview。这是登录信息:
private void postLoginData() throws ClientProtocolException, IOException {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("loginurl"); // Changed for question.
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("sid", "username"));
nameValuePairs.add(new BasicNameValuePair("pin", "pass"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String finalres = inputStreamToString(response.getEntity().getContent()).toString();
tvStatus.setText(finalres);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
这是inputStreamToString()
private StringBuilder inputStreamToString(InputStream is) throws IOException {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
while ((line = rd.readLine()) != null) {
total.append(line);
}
// Return full string
return total;
}
问题是它总是只返回登录页面的HTML。当用户在网站上登录失败时,它会显示一条消息来指示。即使我添加了不正确的凭据,它也不会显示任何不同的内容。同样,如果我添加正确的登录名,它仍然只显示登录页面HTML。
答案 0 :(得分:2)
检查HTTP状态。做这样的事情
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//Do Something here.. I'm logged in.
} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
// Do Something here. Access Denied.
} else {
// IF BOTH CASES not found e.g (unknown host and etc.)
}
这将完全按照您要检查状态的方式运行。感谢
答案 1 :(得分:1)
我猜问题与this question类似。看看它,有一些解决方案,可能对你解决它。