0

I have a login form currently taking login parameters and logging into a website using HTTP Post Request. I am unsure of the server type so that could be the problem. Once it takes the login credentials, it coverts the inputstream to a string (all the html) and sets that to a textview. Here's the login:

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
    }
}

And here's the 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;
}

The problem is that it ALWAYS just returns the HTML for the login page. When a user fails login on the site, it has a little message to indicate so. Even if I add incorrect credentials, it doesn't display anything different. Likewise, if I add the correct login, it still shows me just the login page HTML.

Mark Lyons
  • 1,324
  • 5
  • 20
  • 54

2 Answers2

2

To check for HTTP status. Do something like this

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.)
}

This will exactly works as you want to check for status. thanks

Hamza Waqas
  • 629
  • 4
  • 12
1

I guess the problem is similar to this question. Check it out, there are some solutions, which might work for you in solving it.

Community
  • 1
  • 1
noob
  • 18,447
  • 20
  • 113
  • 179