Question

I'm trying to login to a web page by sending the username/pass using post method. I managed to do it successfully, but what I'm trying to achieve is to know whether I logged in successfully or no ( Programmatically in Java ). My approach was to get the source of the html page as a string and start looking for the word "Logged in as" to determine if the logging process has been completed successfully or no. I tried doing so and show the string in the Logcat, but the string seems too long to be shown.

So is this even possible ? and if so, is this the only approach to achieve the required ?

Thanks.

No correct solution

OTHER TIPS

usually when you do a post http you do it with an async task, if that is the case, then you need to override a method called onpostexecute which will let you do the handling.and change a (usually static) flag to know the user is logged now.

If you are using an ASyncTask to doing your network calls then you can return a string, this string will contain the response you got from the server you are trying to connect to. This all runs in the background via doInBackground()

In the onPostExecute() function which runs on the main thread, so you can check if the string contains whatever the text is for a successful login and an unsuccessful login.

if (login.contains("success") {
    textView.setText("You have logged in");
else if (login.contains("failed") {
    textView.setText("Login failed, please try again later");

The HttpPost you do should return something from the server. You that information to do whatever you need. The best way to do it is to get your server to return a JSON response which you can parse and then get the details

When your android app sends data to the server, the server should send back a response.

JSON format is very common nowadays.

PHP is a good tool to manipulate your data and response.

Here is a example on how you would do that

$response = array();

if($LoginSuccessfull){
  $response = array('success'=> true, 'message'=> 'logged in successfully';
}else{
  $response = array('success'=> false, 'message'=> 'loggin failure';
}

echo json_encode($response);

From your httpClient, you can check if a cookie was set (assuming you are using cookies to authenticate your users), you get a list of cookies from this code:

List<Cookie> cookies = client.getCookieStore().getCookies();

and check if the cookie exists

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top