HttpClient 设置用户凭证

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

1)创建一个 CredentialsProvider 对象

该CredentialsProvider接口维持收集保存用户的登录凭据。您可以通过实例化BasicCredentialsProvider类(此接口的默认实现)来创建其对象。

CredentialsProvider credentialsPovider = new BasicCredentialsProvider();

2)设置凭证

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

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

  • AuthScope 对象: 指定详细信息的身份验证范围,如主机名、端口号和身份验证方案名称。

  • Credentials 对象:指定凭据(用户名、密码)。

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)设置 credentialsPovider

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

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

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

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

HttpClient设置用户凭证的完整示例

以下是一个示例程序,它演示了针对需要用户身份验证的目标站点执行 HTTP 请求。

package com.yiidian;

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

      //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());
      }
   }
}

输出结果为:

热门文章

优秀文章