lugeno
lugeno

Reputation: 1065

Android - Problems with consecutive HttpPosts

I'm writing a simple HttpClient in Android because I need to make various POST requests in succession. I first do a HttpGet and than the fisrt HttpPost. I can retrieve the HTML of the first GET and of the first POST. But if I do a new GET or a new POST I obtain a blank response. To clarify my problem I attach the code.

    DefaultHttpClient httpclient = new DefaultHttpClient();

    //FIRST GET TO ACCESS LOGIN MODULE

    try {
        HttpGet httpget = new HttpGet("https://site/link_to_access_the_login_form");

        HttpResponse response = null;
        try {
            response = httpclient.execute(httpget);
        } catch (ClientProtocolException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        HttpEntity entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        entity.consumeContent();


        //FIRST POST TO ACCESS THE RESTRICTED AREA

        HttpPost httpost = new HttpPost("https://site/login/login.do");

        List <NameValuePair> nameValuePairs = new ArrayList <NameValuePair>(6);
        nameValuePairs.add(new BasicNameValuePair("login", "uid"));
        nameValuePairs.add(new BasicNameValuePair("password", "pwd"));
        //additional params


        httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

        try {
            response = httpclient.execute(httpost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        //entity.consumeContent();

        try {
            String responseTextPost1 = EntityUtils.toString(entity);
            entity.consumeContent();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //SECOND POST TO ACCESS A LINK IN THE RESTRICTED AREA

        httpost = new HttpPost("https://site/role/script.do");
        List <NameValuePair> nameValuePairs6 = new ArrayList <NameValuePair>(6);
        //Parameters...

        httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs6, HTTP.UTF_8));

        try {
            response = httpclient.execute(httpost);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("Login form get: " + response.getStatusLine());
        entity = response.getEntity();


        try {
            String responseTextPost2 = EntityUtils.toString(entity);
            entity.consumeContent();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    } catch..............

responseTextPost2 seems to be blank. Suggestions on how to manage this problem?

Upvotes: 0

Views: 507

Answers (2)

user3143899
user3143899

Reputation: 1

HttpParams httpParams=new BasicHttpParams();
DefaultHttpClient httpclient = new DefaultHttpClient(httpParams);

//FIRST GET TO ACCESS LOGIN MODULE

try {
    HttpGet httpget = new HttpGet("https://site/link_to_access_the_login_form");

    HttpResponse response = null;
    try {
        response = httpclient.execute(httpget);
    } catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    HttpEntity entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    entity.consumeContent();


    //FIRST POST TO ACCESS THE RESTRICTED AREA

    HttpPost httpost = new HttpPost("https://site/login/login.do");

    List <NameValuePair> nameValuePairs = new ArrayList <NameValuePair>(6);
    nameValuePairs.add(new BasicNameValuePair("login", "uid"));
    nameValuePairs.add(new BasicNameValuePair("password", "pwd"));
    //additional params


    httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));

    try {
        response = httpclient.execute(httpost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    //entity.consumeContent();

    try {
        String responseTextPost1 = EntityUtils.toString(entity);
        entity.consumeContent();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //SECOND POST TO ACCESS A LINK IN THE RESTRICTED AREA

    httpost = new HttpPost("https://site/role/script.do");
    List <NameValuePair> nameValuePairs6 = new ArrayList <NameValuePair>(6);
    //Parameters...

    httpost.setEntity(new UrlEncodedFormEntity(nameValuePairs6, HTTP.UTF_8));

    try {
        response = httpclient.execute(httpost);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    System.out.println("Login form get: " + response.getStatusLine());
    entity = response.getEntity();


    try {
        String responseTextPost2 = EntityUtils.toString(entity);
        entity.consumeContent();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


} catch..............

Upvotes: 0

lugeno
lugeno

Reputation: 1065

For posterity...

The problem is the name of NameValuePairs variables. They must have the same name. So both "nameValuePairs".

Hope this helps!

Upvotes: 1

Related Questions