Apache Httpclient 简明教程

Apache HttpClient - User Authentication

使用 HttpClient,您可以连接到需要用户名和密码的网站。本章说明了如何针对询问用户名和密码的网站执行客户端请求。

Step 1 - Create a CredentialsProvider object

CredentialsProvider 接口维护一个用于保存用户登录凭据的集合。通过实例化 BasicCredentialsProvider 类(此接口的默认实现),您可以创建其对象。

CredentialsProvider credentialsPovider = new BasicCredentialsProvider();

Step 2 - Set the Credentials

可以使用 setCredentials() 方法将所需凭据设置为 CredentialsProvider 对象。

此方法接受两个对象,如下所示 -

  1. AuthScope object − 身份验证范围指定详细信息,如主机名、端口号和身份验证方案名称。

  2. Credentials object - 指定凭据(用户名、密码)。

使用 setCredentials() 方法为主机和代理设置凭据,如下所示 -

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

Step 3 - Create a HttpClientBuilder Object

使用 HttpClients 类的 custom() 方法创建一个 HttpClientBuilder

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

Step 4 - Set the credentialsPovider

可以使用 setDefaultCredentialsProvider() 方法将上面创建的 credentialsPovider 对象设置到 HttpClientBuilder。

通过将此前步骤中创建的 CredentialProvider 对象传递给 CredentialsProvider object() 方法,将其设置为客户端构建器,如下所示。

clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);

Step 5 - Build the CloseableHttpClient

使用 HttpClientBuilder 类的 build() 方法构建 CloseableHttpClient 对象。

CloseableHttpClient httpclient = clientbuilder.build()

Step 6 - Create a HttpGet object and execute it

通过实例化 HttpGet 类创建一个 HttpRequest 对象。使用 execute() 方法执行此请求。

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

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

Example

以下示例程序演示了针对需要用户身份验证的目标网站执行 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://www.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://www.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());
      }
   }
}

Output

在执行上述程序时,将生成以下输出。

GET
HTTP/1.1 200 OK
200