Java 使用 POST 的 Httpclient 登录 - 两阶段登录?

Java Httpclient login with POST - two stage login?

我正在尝试使用 POST 通过表单登录网站。我可以发送参数,这些参数已被接受,但随后我被重定向到一个页面,其中有其他形式,看起来像这样

出于安全原因删除了内容。

<form name="form" id="forwardParticipantForm" class="box2" method="post" action="https://faturas.portaldasfinancas.gov.pt/painelAdquirente.action">
        <input type="hidden" name="nif" value="......."/>
        <input type="hidden" name="userID" value="......."/>
        <input type="hidden" name="userName" value="......."/>
        <input type="hidden" name="sessionID" value="......."/>
        <input type="hidden" name="tc" value="........"/>
        <input type="hidden" name="tv" value="......."/>
        <input type="hidden" name="partID" value=".......">
        <input type="hidden" name="sign" value="......."/>

而这一页唯一写的是"Login successful. You're being redirected in a secure way to the requested service"。该网站然后自动将用户重定向到 "main page".

但是在我的程序中,连接停留在这个页面中,我无法完成登录,因此,我无法检索到我想要的信息。

重定向不是问题,因为正在跟踪重定向。如果我尝试 GET 一个需要登录的页面,我会再次被重定向到这个页面,但我会收到一个新的 sessionID符号。我还尝试使用我收到的参数在该页面上创建 POST,但同样的事情发生了。

编辑:检查浏览器中的元素,有两个 POST,第一个带有凭据,第二个带有我收到的参数上面的表格。

代码:

BasicCookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpclient = HttpClientBuilder.create().
            setUserAgent("Mozilla/5.0").
            setDefaultCookieStore(cookieStore).
            setRedirectStrategy(new LaxRedirectStrategy()).
            build();


    try {
        //GET
        HttpGet httpGet1 = new HttpGet("https://www.acesso.gov.pt/jsp/loginRedirectForm.jsp?path=painelAdquirente.action&partID=EFPF");
        CloseableHttpResponse response1 = httpclient.execute(httpGet1);

        try {
            HttpEntity entity1 = response1.getEntity();
            System.out.println("Login form get: 1st get " + response1.getStatusLine());
            EntityUtils.consume(entity1);

        } finally {
            response1.close();
        }

        //POST - LOGIN
        HttpPost authPost = new HttpPost("https://www.acesso.gov.pt/jsp/loginRedirect.jsp");
        // Request parameters and other properties.
        List <NameValuePair> params = new ArrayList <NameValuePair>();
        params.add(new BasicNameValuePair("username", "......."));
        params.add(new BasicNameValuePair("password", "......."));
        params.add(new BasicNameValuePair("partID", "EFPF"));
        params.add(new BasicNameValuePair("path", "consultarDocumentosAdquirente.action?dataInicioFilter=2015-08-01"));
        authPost.setEntity(new UrlEncodedFormEntity(params));


        System.out.println();
        CloseableHttpResponse response2 = httpclient.execute(authPost);

        try {
            HttpEntity entity2 = response2.getEntity();
            System.out.println("Login form get: 2nd post " + response2.getStatusLine());
            EntityUtils.consume(entity2);
        } finally {
            response2.close();
        }
    } finally {
        httpclient.close();
    }

我成功登录了。 param partID,因为它不像其他的那样结束( "> 而不是 "/> )因为我正在获取而丢失了一封信它与 line.length()-3.