使用HttpClient,您可以连接到需要用户名和密码的网站.本章介绍如何针对要求输入用户名和密码的站点执行客户端请求.
步骤1 - 创建CredentialsProvider对象
CredentialsProvider Interface维护一个集合以保存用户登录凭据.您可以通过实例化 BasicCredentialsProvider 类来创建其对象,该类是此接口的默认实现.
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
步骤2 - 设置凭据
您可以使用将所需凭据设置为CredentialsProvider对象setCredentials()方法.
此方法接受下面给出的两个对象 :
AuthScope对象 : 身份验证范围指定主机名,端口号和身份验证方案名称等详细信息.
凭据对象 : 指定凭据(用户名,密码).
使用 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 HttpClientBuilderHttpClientBuilder clientbuilder = HttpClients.custom();
步骤4 - 设置credentialsPovider
您可以使用
将上一步创建的CredentialProvider对象设置为客户端构建器,方法是将其传递给 CredentialsProvider对象()方法如下所示.
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
步骤5 - 构建CloseableHttpClient
使用 HttpClientBuilder 类的 build()方法构建 CloseableHttpClient 对象.
CloseableHttpClient httpclient = clientbuilder.build()
步骤6 - 创建HttpGet对象并执行它
通过实例化HttpGet类来创建HttpRequest对象.使用执行()方法执行此请求.
//Creating a HttpGet objectHttpGet httpget = new HttpGet("https://www.IT屋.com/ ");//Executing the Get requestHttpResponse 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://www.IT屋.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.IT屋.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输出
执行时,以上程序生成以下输出.
GET HTTP/1.1 200 OK 200