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