Apache HttpClient - 代理認證



本章節將學習如何建立一個使用使用者名稱和密碼進行身份驗證的 HttpRequest,並透過代理將其傳輸到目標主機,並透過示例進行說明。

步驟 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 物件。將前面建立的 **CredentialsProvider** 物件傳遞給此方法。

clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);

步驟 5 - 構建 CloseableHttpClient

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

CloseableHttpClient httpclient = clientbuilder.build();

步驟 6 - 建立代理和目標主機

透過例項化 **HttpHost** 類來建立目標和代理主機。

//Creating the target and proxy hosts
HttpHost target = new HttpHost("example.com", 80, "http");
HttpHost proxy = new HttpHost("localhost", 8000, "http");

步驟 7 - 設定代理並構建 RequestConfig 物件

使用 **custom()** 方法建立一個 **RequestConfig.Builder** 物件。使用 **setProxy()** 方法將前面建立的 proxyHost 物件設定為 **RequestConfig.Builder**。最後,使用 **build()** 方法構建 **RequestConfig** 物件。

RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
RequestConfig config = reqconfigconbuilder.build();

步驟 8 - 建立 HttpGet 請求物件並將其配置物件設定到其中。

透過例項化 HttpGet 類來建立一個 **HttpGet** 物件。使用 **setConfig()** 方法將上一步中建立的配置物件設定為此物件。

//Create the HttpGet request object
HttpGet httpGet = new HttpGet("/");

//Setting the config to the request
httpget.setConfig(config);

步驟 9 - 執行請求

透過將 HttpHost 物件(目標)和請求(HttpGet)作為引數傳遞給 **execute()** 方法來執行請求。

HttpResponse httpResponse = httpclient.execute(targetHost, httpget);

示例

以下示例演示如何使用使用者名稱和密碼透過代理執行 HTTP 請求。

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
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 ProxyAuthenticationExample {
   public static void main(String[] args) throws Exception {

      //Creating the CredentialsProvider object
      CredentialsProvider credsProvider = new BasicCredentialsProvider();

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

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

      //Setting the credentials
      clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
      
      //Building the CloseableHttpClient object
      CloseableHttpClient httpclient = clientbuilder.build();


      //Create the target and proxy hosts
      HttpHost targetHost = new HttpHost("example.com", 80, "http");
      HttpHost proxyHost = new HttpHost("localhost", 8000, "http");

      //Setting the proxy
      RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
      reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
      RequestConfig config = reqconfigconbuilder.build();

      //Create the HttpGet request object
      HttpGet httpget = new HttpGet("/");

      //Setting the config to the request
      httpget.setConfig(config);
 
      //Printing the status line
      HttpResponse response = httpclient.execute(targetHost, httpget);
      System.out.println(response.getStatusLine());

   }
}

輸出

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

HTTP/1.1 200 OK
廣告