DefaultHttpClient GET and POST commands Java Android

22,741

The response to the logon request contains a Cookie with the session ID for your logged-in session. You must post this back to the server in subsequent requests or else those other requests are not related to your logon.

EDIT: If HttpClient is actually managing your cookies (check against http://hc.apache.org/httpcomponents-client/tutorial/html/statemgmt.html) then... maybe the login is redirecting you somewhere else after login, and you need to follow that redirect in order to grab cookies? Kind of guessing here but it really sounds like you're not completing login.

Share:
22,741
RenegadeAndy
Author by

RenegadeAndy

Updated on July 09, 2022

Comments

  • RenegadeAndy
    RenegadeAndy almost 2 years

    Ok this is my application :

    An Android app to allow me to submit CokeZone codes into CokeZone.co.uk from a mobile app instead of from the website.

    So I wrote this section of code to do the post logon command and then check to see if im logged in after.

    Problem is - the html I get from the homepage after I send the post command is the default - as if im not logged in - so something is going wrong.

    Can anyone please help! Its probably the URL im sending the POST to, or the params within the POST command - I havent done much of this stuff so its probably something obvious.

    Below is my code so far:

     DefaultHttpClient httpclient = new DefaultHttpClient();
    
        HttpGet httpget = new HttpGet(url);
    
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        thisResponse = printPage(entity.getContent());
        Log.e("debug",thisResponse);
        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }
        System.out.println("Initial set of cookies:");
        List<Cookie> cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
    
        HttpPost httpost = new HttpPost("https://secure.cokezone.co.uk/home/blank.jsp?_DARGS=/home/login/login.jsp");
    
        List <NameValuePair> nvps = new ArrayList <NameValuePair>();
        nvps.add(new BasicNameValuePair("_dyncharset", "ISO-8859-1"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.loginFormBean.name","renegadeandy%40gmail.com"));
        nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.loginFormBean.name", "+"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.cookiedUser", "false"));
        nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.cookiedUser", "+"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.loginFormBean.password", "passwordval"));
        nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.loginFormBean.password", "+"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.rememberMe", "yes"));
        nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.rememberMe", "false"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aSuccessURL", "http://www.cokezone.co.uk/home/index.jsp"));
        nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.aSuccessURL", "+"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aErrorURL", "http://www.cokezone.co.uk/home/index.jsphttps://secure.cokezone.co.uk/home/index.jsp"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.aErrorURL", "+"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.explicitLogin", "true"));
        nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.explicitLogin", "+"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.fICLogin", "login"));
        nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.fICLogin", "+"));
        nvps.add(new BasicNameValuePair("/grlp/login/LoginHandler.fICLogin", "LOGIN"));
        nvps.add(new BasicNameValuePair("_D:/grlp/login/LoginHandler.fICLogin", "+"));
        nvps.add(new BasicNameValuePair("_DARGS", "/home/login/login.jsp"));
    
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    
        response = httpclient.execute(httpost);
        entity = response.getEntity();
    
        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
          thisResponse = printPage(entity.getContent());
          entity.consumeContent();
        }
    
        Log.e("debug",thisResponse);
        Log.e("debug","done");
    
        httpget = new HttpGet("http://www.cokezone.co.uk/home/index.jsp");
    
        response = httpclient.execute(httpget);
        entity = response.getEntity();
    
      TextView points = (TextView)findViewById(R.id.points);
      points.setText(getPoints(entity.getContent()).toString());
      debug.setText(thisResponse);
        System.out.println("Post logon cookies:");
        cookies = httpclient.getCookieStore().getCookies();
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
            }
        }
    
  • RenegadeAndy
    RenegadeAndy about 14 years
    DefaultHttpClient takes care of all of this stuff for you though.
  • Sam
    Sam over 12 years
    "The response to the logon request contains a Cookie with the session ID for your logged-in session. You must post this back to the server in subsequent requests or else those other requests are not related to your logon." Can you tell me how can i implement this task, i really need to pass the session-id back to the server please help