开发者

Android HttpClient POST Form Login

after reading couple of resources for making HttpClient construction to perform Login over a form available on a website (POST method), I wrote this method:

public void connect(View v) {
    final String TAG = ">>>>>>>>>>>> Activity Log: ";
    request = new HttpGet("http://www.mysite.com/login");
    try {
        response = client.execute(request);
    } catch (ClientProtocolException e3) {
        e3.printStackTrace();
    } catch (IOException e3) {
        e3.printStackTrace();
    }

    entity = response.getEntity();
    Log.d(TAG, "Login form get: " + response.getStatusLine());
    if(entity != null) {
        try {
            entity.consumeContent();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Log.d(TAG, "Initial set of cookies:");

    cookies = client.getCookieStore().getCookies();
    if (cookies.isEmpty())
    {
        Log.d(TAG, "None");
    }
    else
    {
        for(int i = 0; i<cookies.size(); i++)
        {
            Log.d(TAG, "- " + cookies.get(i));
        }
    }
    String action = "/login.php";
    String yourServer = "http://www.mysite.com";
    post = new HttpPost(yourServer + action);

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("username", "myuser"));
    params.add(new BasicNameValuePair("password", "mypass"));

    try {
        post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
    } catch (UnsupportedEncodingException e2) {
        e2.printStackTrace();
    }


    try {
        response = client.execute(post);
    } catch (ClientProtocolException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    entity = response.getEntity();

    Log.d(TAG, "Login form get: " + response.getStatusLine());
    if(entity != null){
        try {
            entity.consumeContent();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    Log.d(TAG, "Post logon cookies:");
    cookies = client.getCookieStore().getCookies();
    if (cookies.isEmpty())
    {
        Log.d(TAG, "None");
    } 
    else
    {
        for (int i = 0; i < cookies.size(); i++) {
            L开发者_C百科og.d(TAG, "- " + cookies.get(i));
        }
    }
}

But whatever I put as username/password I get Status: "OK 200". And as cookie I get something like this:

[version: 0][name: PHPSESSID][value: 9ismhf3p5c282p1east0drme02][domain: .mysite.com][path: /][expiry: null]

When I try to access some URL that is not available for not logged in users, I get the login form page. How do I know I've made a successful login and access the unavailable URLs?


Hey, I had the same problem. You are getting Status: "OK 200" because the post is deemed successful, regardless of the outcome of the login. My solution was to inspect the response entity. Essentially, this is a string containing the source of the page that your post would send a user to if done in a browser. Within this will be the page title. For example: Login Failed. While not always as aptly named as this, you can easily find the titles of both a successful login page and an unsuccessful login page by simply logging in manually and then using view -> page source.

So, the solution would contain something like this:

final String responseText =  EntityUtils.toString(response.getEntity());
if (responseText.contains("<title>Login Failed<title>")
{
You failed
}

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜