使用HTTPClient库进行Java表单登录

使用HTTPClient库进行Java表单登录,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我正试图编写一个简单的Java程序,使用HTTPClient库登录到一个站点。问题是我被重定向到错误的凭据页面。据我所知,我已经捕获了cookies和隐藏字段,并通过POST方法将它们传递回去。下面是一段代码,我感谢您的任何评论或提示 public class ClientFormLogin { private static final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] ar

我正试图编写一个简单的Java程序,使用HTTPClient库登录到一个站点。问题是我被重定向到错误的凭据页面。据我所知,我已经捕获了cookies和隐藏字段,并通过POST方法将它们传递回去。下面是一段代码,我感谢您的任何评论或提示

 public class ClientFormLogin {
    private static final String USER_AGENT = "Mozilla/5.0";

    public static void main(String[] args) throws Exception {
        BasicCookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .build();

        try {
            HttpGet httpget = new HttpGet("http://dubai.dubizzle.com/");
            httpget.addHeader("User-Agent", USER_AGENT);
            CloseableHttpResponse getResponse = httpclient.execute(httpget);
            try {
                HttpEntity entity = getResponse.getEntity();

                System.out.println("GET: " + getResponse.getStatusLine());
                EntityUtils.consume(entity);

                List<Cookie> cookies = cookieStore.getCookies();
                System.out.println("Get cookies:");
                if (cookies.isEmpty()) {
                    System.out.println("None");
                } else {
                    for (int i = 0; i < cookies.size(); i++) {
                        Cookie cookie =  cookies.get(i);
                        System.err.println(
                                "Cookie: " + cookie.getName() +
                                ", Value: " + cookie.getValue() +
                                ", IsPersistent?: " + cookie.isPersistent() +
                                ", Expiry Date: " + cookie.getExpiryDate() +
                                ", Comment: " + cookie.getComment()); 
                    }
                }
            } finally {
                getResponse.close();
            }

            HttpPost httpPost = new HttpPost("http://dubai.dubizzle.com/accounts/login/");
            List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
            urlParameters.add(new BasicNameValuePair("userName", "metakova@gmail.com"));
            urlParameters.add(new BasicNameValuePair("password", "Meta_123"));
            urlParameters.add(new BasicNameValuePair("next", "/"));

            HttpEntity postParams = new UrlEncodedFormEntity(urlParameters, Consts.UTF_8);
            httpPost.setEntity(postParams);

            CloseableHttpResponse postResponse = httpclient.execute(httpPost);
            System.out.println("POST: " + postResponse.getStatusLine());

            List<Cookie> postCookies = cookieStore.getCookies();
            if (postCookies.isEmpty()) {
                System.out.println("None");
            } else {
                System.out.println("Post Cookies:");
                for (int i = 0; i < postCookies.size(); i++) {
                    Cookie cookie =  postCookies.get(i);
                    System.err.println(
                            "Cookie: " + cookie.getName() +
                            ", Value: " + cookie.getValue() +
                            ", IsPersistent?: " + cookie.isPersistent() +
                            ", Expiry Date: " + cookie.getExpiryDate() +
                            ", Comment: " + cookie.getComment()); 
                }
            }


            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                   postResponse.getEntity().getContent()));

                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = reader.readLine()) != null) {
                    if(!inputLine.matches("^\\s*$")){
                        response.append(inputLine);
                        response.append("\n");
                    }
                }
                reader.close();
                // Print Response Content 
                System.out.println(response);

            } finally {
                postResponse.close();
            }
        } finally {
            httpclient.close();
        }
    }
}

您在哪里创建cookie?是否不想在post客户端上设置cookie存储?这与此无关HtmlUnit@roby谢谢我已经更新了代码,但问题仍然存在。
Jun 21, 2015 2:11:53 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: skybar_sess_False=1; Domain=.dubizzle.com; expires=Mon, 22 Jun 2015 10:11:52 GMT; Path=/". Invalid 'expires' attribute: Mon, 22 Jun 2015 10:11:52 GMT
GET: HTTP/1.1 200 OK
Get cookies:
Cookie: default_site, Value: 2, IsPersistent?: true, Expiry Date: Mon Jun 20 14:11:52 GST 2016, Comment: null
Cookie: sid, Value: 37225e477ebc9230335627fd6dffeb43, IsPersistent?: true, Expiry Date: Sun Jun 28 14:11:52 GST 2015, Comment: null
Jun 21, 2015 2:11:53 PM org.apache.http.client.protocol.ResponseProcessCookies processCookies
WARNING: Invalid cookie header: "Set-Cookie: skybar_sess_False=1; Domain=.dubizzle.com; expires=Mon, 22 Jun 2015 10:11:53 GMT; Path=/". Invalid 'expires' attribute: Mon, 22 Jun 2015 10:11:53 GMT
POST: HTTP/1.1 200 OK
Post Cookies:
Cookie: default_site, Value: 2, IsPersistent?: true, Expiry Date: Mon Jun 20 14:11:53 GST 2016, Comment: null
Cookie: sid, Value: 37225e477ebc9230335627fd6dffeb43, IsPersistent?: true, Expiry Date: Sun Jun 28 14:11:53 GST 2015, Comment: null