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.com/ ");

//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.com/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.com/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
广告