Android - 连续 HttpPost 的问题

发布于 2024-12-15 13:42:20 字数 3423 浏览 3 评论 0原文

我正在 Android 中编写一个简单的 HttpClient,因为我需要连续发出各种 POST 请求。我首先执行 HttpGet,然后执行第一个 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 似乎是空白的。关于如何解决这个问题的建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气称霸 2024-12-22 13:42:20

对于后代...

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

希望这有帮助!

For posterity...

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

Hope this helps!

魔法唧唧 2024-12-22 13:42:20
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..............
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..............
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文