Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache HttpClient 4.2.1成功登录后填写表单的POST请求_Java_Apache_Http Post_Apache Httpclient 4.x - Fatal编程技术网

Java Apache HttpClient 4.2.1成功登录后填写表单的POST请求

Java Apache HttpClient 4.2.1成功登录后填写表单的POST请求,java,apache,http-post,apache-httpclient-4.x,Java,Apache,Http Post,Apache Httpclient 4.x,我正在写一个应用程序,可以登录到一个网站,并提交某种形式。 我可以成功登录,但当我尝试发送另一个POST请求以提交另一个表单时,什么也没有发生 这是我的代码: try { //log in to site HttpPost httpPost = new HttpPost("http://mysite.ru"); List<NameValuePair> nvps = new ArrayList<NameValuePai

我正在写一个应用程序,可以登录到一个网站,并提交某种形式。 我可以成功登录,但当我尝试发送另一个POST请求以提交另一个表单时,什么也没有发生

这是我的代码:

    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();
    }
试试看{
//登录到站点
HttpPost HttpPost=新的HttpPost(“http://mysite.ru");
List nvps=new ArrayList();
添加(新的BasicNameValuePair(“登录”、“来宾”);
添加(新的BasicNameValuePair(“密码”、“来宾”);
setEntity(新的UrlEncodedFormEntity(nvps,Consts.UTF_8));
HttpResponse response=httpClient.execute(httpPost);
HttpEntity=response.getEntity();
EntityUtils.consume(实体);
//好的,我得到了饼干
列表cookies=httpClient.getCookieStore().getCookies();
if(cookies.isEmpty()){
系统输出打印项次(“无”);
}否则{
对于(int i=0;i

第二种形式与第一种形式在类型上没有区别。

我解决了我的问题。在第二个表单(不是登录表单)中,有提交按钮:

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

我不知道为什么我在填写授权表时不需要发送这样一个按钮值来登录。但现在一切都在运转

尝试启用wire logging并确保您的表单按预期提交:非常感谢!美沙酮也有同样的问题!我自己也面临同样的问题。再次感谢。:)
nvps2.add(new BasicNameValuePair("apply", "Save"));