2015-09-23 48 views
0

我正在嘗試使用POST登錄網站。我可以發送參數,這些參數已被接受,但隨後我被重定向到一個頁面,其中有其他形式,如下所示使用POST的Java Httpclient登錄 - 兩階段登錄?

出於安全原因已刪除內容。

<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="......."/> 

此頁面唯一寫的是「登錄成功,您正以安全的方式被重定向到請求的服務」。網站會自動將用戶重定向到「主頁面」。

但是在我的程序中,連接站在這個頁面上,我無法完成登錄,因此我無法檢索到我想要的信息。

隨着重定向的出現,這不是重定向的問題。如果我嘗試GET需要登錄的頁面,我將再次重定向到此頁面,但我收到新的會話ID標記。我也嘗試使用我收到的參數但是發生同樣的事情來製作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(); 
    } 
+0

添加您使用的代碼 –

+0

代碼添加到OP – MrChipzz

回答

0

我設法正常登錄。參數partID,因爲它不像其他的那樣結束(「>而不是」/>),因爲我正在使用line.length() - 3獲取它,所以丟失了一個字母。