Apache HttpClient - 代理认证

  • 简述

    在本章中,我们将学习如何创建一个使用用户名和密码进行身份验证的 HttpRequest,并使用一个示例将其通过代理传送到目标主机。
  • 第 1 步 - 创建 CredentialsProvider 对象

    CredentialsProvider 接口维护一个集合来保存用户登录凭据。您可以通过实例化该接口的默认实现 BasicCredentialsProvider 类来创建其对象。
    
    CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
    
  • 第 2 步 - 设置凭据

    您可以使用 setCredentials() 方法为 CredentialsProvider 对象设置所需的凭据。此方法接受两个对象 -
    • AuthScope object - 指定主机名、端口号和身份验证方案名称等详细信息的身份验证范围。
    • 凭据对象 - 指定凭据(用户名、密码)。使用 setCredentials() 方法为主机和代理设置凭据,如下所示。
    
    credsProvider.setCredentials(new AuthScope("example.com", 80), new
       UsernamePasswordCredentials("user", "mypass"));
    credsProvider.setCredentials(new AuthScope("localhost", 8000), new
       UsernamePasswordCredentials("abc", "passwd"));
    
  • 第 3 步 - 创建一个 HttpClientBuilder 对象

    使用 HttpClientscustom() 方法创建一个 HttpClientBuilder类如下图-
    
    //创建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 类来创建目标和代理主机。
    
    //创建目标和代理主机
    HttpHost 目标 = new HttpHost("example.com", 80, "http");
    HttpHost 代理 = 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 配置 = reqconfigconbuilder.build();
    
  • 第 8 步 - 创建一个 HttpGet 请求对象并为其设置配置对象。

    通过实例化 HttpGet 类创建一个 HttpGet 对象。使用 setConfig() 方法将上一步创建的配置对象设置为此对象。
    
    //创建HttpGet请求对象
    HttpGet httpGet = new HttpGet("/");
    //将配置设置为请求
    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