Apache HttpClient - 基於表單的登入



使用 HttpClient 庫,您可以傳送請求或透過傳遞引數登入到表單。

請按照以下步驟登入表單。

步驟 1 - 建立 HttpClient 物件

HttpClients 類的 createDefault() 方法返回 CloseableHttpClient 類的物件,它是 HttpClient 介面的基本實現。使用此方法,建立 HttpClient 物件:

CloseableHttpClient httpClient = HttpClients.createDefault();

步驟 2 - 建立 RequestBuilder 物件

RequestBuilder 類用於透過向其新增引數來構建請求。如果請求型別為 PUT 或 POST,它會將引數作為 URL 編碼實體新增到請求中。

使用 post() 方法建立一個 RequestBuilder 物件(POST 型別)。

//Building the post request object
RequestBuilder reqbuilder = RequestBuilder.post();

步驟 3 - 將 Uri 和引數設定到 RequestBuilder。

使用 RequestBuilder 類的 setUri()addParameter() 方法將 URI 和引數設定到 RequestBuilder 物件。

//Set URI and parameters
RequestBuilder reqbuilder = reqbuilder.setUri("http://httpbin.org/post");
reqbuilder = reqbuilder1.addParameter("Name", "username").addParameter("password", "password");

步驟 4 - 構建 HttpUriRequest 物件

設定所需引數後,使用 build() 方法構建 HttpUriRequest 物件。

//Building the HttpUriRequest object
HttpUriRequest httppost = reqbuilder2.build();

步驟 5 - 執行請求

CloseableHttpClient 物件的 execute 方法接受一個 HttpUriRequest(介面)物件(即 HttpGet、HttpPost、HttpPut、HttpHead 等)並返回一個響應物件。

透過將其傳遞給 execute() 方法,執行在前面步驟中建立的 HttpUriRequest。

//Execute the request
HttpResponse httpresponse = httpclient.execute(httppost);

示例

以下示例演示瞭如何透過傳送登入憑據來登入表單。在這裡,我們向表單傳送了兩個引數 - 使用者名稱和密碼,並嘗試列印請求的訊息實體和狀態。

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;

public class FormLoginExample {
 
   public static void main(String args[]) throws Exception {

      //Creating CloseableHttpClient object
      CloseableHttpClient httpclient = HttpClients.createDefault();
 
      //Creating the RequestBuilder object
      RequestBuilder reqbuilder = RequestBuilder.post();

      //Setting URI and parameters
      RequestBuilder reqbuilder1 = reqbuilder.setUri("http://httpbin.org/post");
      RequestBuilder reqbuilder2 = reqbuilder1.addParameter("Name", 
         "username").addParameter("password", "password");

      //Building the HttpUriRequest object
      HttpUriRequest httppost = reqbuilder2.build();

      //Executing the request
      HttpResponse httpresponse = httpclient.execute(httppost);

      //Printing the status and the contents of the response
      System.out.println(EntityUtils.toString(httpresponse.getEntity()));
      System.out.println(httpresponse.getStatusLine());
   }
}

輸出

執行上述程式後,將生成以下輸出:

{
   "args": {},
   "data": "",
   "files": {},
   "form": {
      "Name": "username",
      "password": "password"
   },
   "headers": {
      "Accept-Encoding": "gzip,deflate",
      "Connection": "close",
      "Content-Length": "31",
      "Content-Type": "application/x-www-form-urlencoded; charset = UTF-8",
      "Host": "httpbin.org",
      "User-Agent": "Apache-HttpClient/4.5.6 (Java/1.8.0_91)"
   },
   "json": null,
   "origin": "117.216.245.180",
   "url": "http://httpbin.org/post"
}
HTTP/1.1 200 OK

帶 Cookie 的表單登入

如果您的表單儲存 Cookie,則不要建立預設的 CloseableHttpClient 物件。

透過例項化 BasicCookieStore 類來建立一個 CookieStore 物件。

//Creating a BasicCookieStore object
BasicCookieStore cookieStore = new BasicCookieStore();

使用 HttpClients 類的 custom() 方法建立一個 HttpClientBuilder。

//Creating an HttpClientBuilder object
HttpClientBuilder clientbuilder = HttpClients.custom();

使用 setDefaultCookieStore() 方法將 Cookie 儲存設定到客戶端構建器。

//Setting default cookie store to the client builder object
Clientbuilder = clientbuilder.setDefaultCookieStore(cookieStore); 

使用 build() 方法構建 CloseableHttpClient 物件。

//Building the CloseableHttpClient object
CloseableHttpClient httpclient = clientbuilder1.build();

如上所述構建 HttpUriRequest 物件,並透過執行請求傳遞。

如果頁面儲存 Cookie,則您傳遞的引數將新增到 Cookie 儲存中。

您可以列印 CookieStore 物件的內容,在其中您可以看到您的引數(以及頁面在情況下儲存的先前引數)。

要列印 Cookie,請使用 getCookies() 方法從 CookieStore 物件獲取所有 Cookie。此方法返回一個 List 物件。使用迭代器,列印列表物件的如下內容:

//Printing the cookies
List list = cookieStore.getCookies();

System.out.println("list of cookies");
Iterator it = list.iterator();
if(it.hasNext()) {
   System.out.println(it.next());
}
廣告

© . All rights reserved.