DefaultGttpClient GET and POST Commands Java Android

Ok, this is my application:

Android app that allows me to send CokeZone codes to CokeZone.co.uk from a mobile app, not a website.

So, I wrote this section of code to execute a command after logging in and then check if I was logged in after it.

The problem is that the html I get from the home page after I send the post command is the default - as if I'm not logged in - so something is going wrong.

Can anyone help! Probably the url sending the POST, or the parameters in the POST command - I haven't done much of this, so it's probably something obvious.

Below is my code:

 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());
        }
    }

      

+2


source to share


1 answer


The login response contains a Session ID Cookie for the login session. You have to send this back to the server in subsequent requests, otherwise these other requests are not related to your login.



EDIT: If HttpClient actually manages your cookies (check http://hc.apache.org/httpcomponents-client/tutorial/html/statemgmt.html ), then ... maybe the login redirects you somewhere else after you login system and you need to follow this redirect to grab cookies? Kind of guessing here, but it looks like you are not completing your login.

+3


source







All Articles