Apache HttpClient - 使用者認證



使用 HttpClient,您可以連線到需要使用者名稱和密碼的網站。本章解釋如何對需要使用者名稱和密碼的站點執行客戶端請求。

步驟 1 - 建立 CredentialsProvider 物件

CredentialsProvider 介面維護一個集合來儲存使用者登入憑據。您可以透過例項化BasicCredentialsProvider 類來建立其物件,這是該介面的預設實現。

CredentialsProvider credentialsPovider = new BasicCredentialsProvider();

步驟 2 - 設定憑據

您可以使用setCredentials() 方法將所需的憑據設定為 CredentialsProvider 物件。

此方法接受如下所示的兩個物件:

  • AuthScope 物件 - 身份驗證範圍,指定主機名、埠號和身份驗證方案名稱等詳細資訊。

  • Credentials 物件 - 指定憑據(使用者名稱、密碼)。

如下所示,使用setCredentials() 方法為主機和代理設定憑據:

credsProvider.setCredentials(new AuthScope("example.com", 80), 
   new UsernamePasswordCredentials("user", "mypass"));
credsProvider.setCredentials(new AuthScope("localhost", 8000), 
   new UsernamePasswordCredentials("abc", "passwd"));

步驟 3 - 建立 HttpClientBuilder 物件

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

//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();

步驟 4 - 設定 credentialsProvider

您可以使用setDefaultCredentialsProvider() 方法將上面建立的 credentialsProvider 物件設定為 HttpClientBuilder。

將上一步中建立的 CredentialProvider 物件傳遞給CredentialsProvider 物件() 方法,將其設定為客戶端構建器,如下所示。

clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);

步驟 5 - 構建 CloseableHttpClient

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

CloseableHttpClient httpclient = clientbuilder.build()

步驟 6 - 建立 HttpGet 物件並執行它

透過例項化 HttpGet 類來建立一個 HttpRequest 物件。使用execute() 方法執行此請求。

//Creating a HttpGet object
HttpGet httpget = new HttpGet("https://tutorialspoint.tw/ ");

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

示例

以下是一個示例程式,演示了對需要使用者身份驗證的目標站點執行 HTTP 請求。

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

public class UserAuthenticationExample {
   
   public static void main(String args[]) throws Exception{
      
      //Create an object of credentialsProvider
      CredentialsProvider credentialsPovider = new BasicCredentialsProvider();

      //Set the credentials
      AuthScope scope = new AuthScope("https://tutorialspoint.tw/questions/", 80);
      
      Credentials credentials = new UsernamePasswordCredentials("USERNAME", "PASSWORD");
      credentialsPovider.setCredentials(scope,credentials);

      //Creating the HttpClientBuilder
      HttpClientBuilder clientbuilder = HttpClients.custom();

      //Setting the credentials
      clientbuilder = clientbuilder.setDefaultCredentialsProvider(credentialsPovider);

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

      //Creating a HttpGet object
      HttpGet httpget = new HttpGet("https://tutorialspoint.tw/questions/index.php");

      //Printing the method used
      System.out.println(httpget.getMethod()); 

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

      //Printing the status line
      System.out.println(httpresponse.getStatusLine());
      int statusCode = httpresponse.getStatusLine().getStatusCode();
      System.out.println(statusCode);

      Header[] headers= httpresponse.getAllHeaders();
      for (int i = 0; i<headers.length;i++) {
         System.out.println(headers[i].getName());
      }
   }
}

輸出

執行上述程式後,會生成以下輸出。

GET
HTTP/1.1 200 OK
200
廣告