本文介绍了阿帕奇的HttpClient 4.2.1 POST请求,以填补成功登录后形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写这可以登录一个网站,并提交某种形式的应用程序。
我可以成功登录,但是当我尝试发送另一个POST请求提交到另一种形式,没有任何反应。

I'm writing an application which can log in to a site and submit a certain form.I can successfully log in, but when I try to sent another POST request to submit another form, nothing happens.

这是我的code:

    try {
           //log in to site
        HttpPost httpPost = new HttpPost("http://mysite.ru");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("login", "guest"));
        nvps.add(new BasicNameValuePair("password", "guest"));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));

        HttpResponse response = httpClient.execute(httpPost);

        HttpEntity entity = response.getEntity();
        EntityUtils.consume(entity);

           //all ok. i obtained cookie
        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());
                ConnectionManager.printLog("- " + cookies.get(i).toString());
            }
        }

            //then trying to fill another form in another page of this site
        httpPost = new HttpPost("http://mysite.ru/?h[0][mode]=controllers&h[0][action]=add&h[1][mode]=controllers");

        List<NameValuePair> nvps2 = new ArrayList<NameValuePair>();
        nvps2.add(new BasicNameValuePair("owner", "0"));
        nvps2.add(new BasicNameValuePair("imei", "123456789123456"));
        nvps2.add(new BasicNameValuePair("password", "asdfghj"));
        nvps2.add(new BasicNameValuePair("type_id", "6"));
        nvps2.add(new BasicNameValuePair("remarks", ""));

        httpPost.setEntity(new UrlEncodedFormEntity(nvps2));

        response = httpClient.execute(httpPost);
           //after filling this form, site must redirect me on another page. 
        entity = response.getEntity();
           //but then I look on page I obtained, it's still the same page with form I tried to fill. 
           //It seems like I didn't post request.
        String pageHTML = EntityUtils.toString(entity);
        System.out.println(pageHTML);

        EntityUtils.consume(entity);

    } finally {
        httpClient.getConnectionManager().shutdown();
    }

第二种形式不按类型从第一不同。

The second form doesn't differ from first by type.

推荐答案

我解决我的问题。在第二种形式(不登录表单)有提交按钮:

I solved my problem. In second form (not the login form) there was submit Button:

<form method="post">
   <p>...<p>
   <p>...<p>
   <p>...<p>
   <p>...<p>
   <p>...<p>
     <input type="submit" name="apply" value "Save">
</form>

要填写表格,保存并进入下一个页面,我应该再加上一个 ValuePair 来我的帖子的要求:

To fill the form, save and go to the next page I should add one more ValuePair to my post request:

nvps2.add(new BasicNameValuePair("apply", "Save"));

我不知道为什么我没必要发出这样一个按钮值登录时,我充满了授权书。但是,现在一切工作!

I don't know why I didn't need to send such a Button value to log in when I filled the authorization form. But now all is working!

这篇关于阿帕奇的HttpClient 4.2.1 POST请求,以填补成功登录后形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:37