Android - 连续HttpPosts的问题

时间:2011-11-11 16:26:58

标签: android connection httpclient

我在Android中编写一个简单的HttpClient,因为我需要连续发出各种POST请求。我首先做一个HttpGet而不是fisrt HttpPost。 我可以检索第一个GET和第一个POST的HTML。但是如果我进行新的GET或新的POST,我会得到一个空白的回复。为了澄清我的问题,我附上了代码。

    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 似乎是空白的。关于如何管理这个问题的建议?

2 个答案:

答案 0 :(得分:1)

为后人......

问题是NameValuePairs变量的名称。它们必须具有相同的名称。所以都是“nameValuePairs”。

希望这有帮助!

答案 1 :(得分:0)

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