Apache Httpclient 简明教程
Apache HttpClient - Proxy Authentication
在本章中,我们将学习如何创建一个经过用户名和密码身份验证并通过代理将其传送到目标主机的 HttpRequest,并使用一个示例。
Step 1 - Create a CredentialsProvider object
CredentialsProvider 接口维护一个用于保存用户登录凭据的集合。您可以通过实例化 BasicCredentialsProvider 类(此接口的默认实现)来创建其对象。
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
Step 2 - Set the credentials
您可以使用 setCredentials() 方法为 CredentialsProvider 对象设置所需的凭据。此方法接受两个对象 −
-
AuthScope object − 身份验证范围指定详细信息,如主机名、端口号和身份验证方案名称。
-
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 an HttpClientBuilder object
使用 HttpClients 类的 custom() 方法创建一个 HttpClientBuilder ,如下所示。
//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();
Step 4 - Set the CredentialsProvider
可以使用 setDefaultCredentialsProvider() 方法将CredentialsProvider对象设置到HttpClientBuilder对象。将先前创建的 CredentialsProvider 对象传递到此方法中。
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
Step 5 - Build the CloseableHttpClient
使用 build() 方法构建 CloseableHttpClient 对象。
CloseableHttpClient httpclient = clientbuilder.build();
Step 6 - Create the proxy and target hosts
通过实例化 HttpHost 类创建目标和代理主机。
//Creating the target and proxy hosts
HttpHost target = new HttpHost("example.com", 80, "http");
HttpHost proxy = new HttpHost("localhost", 8000, "http");
Step 7 - Set the proxy and build a RequestConfig object
使用 custom() 方法创建一个 RequestConfig.Builder 对象。使用 setProxy() 方法将先前创建的proxyHost对象设置为 RequestConfig.Builder 。最后,使用 build() 方法构建 RequestConfig 对象。
RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
RequestConfig config = reqconfigconbuilder.build();
Step 8 - Create a HttpGet request object and set config object to it.
通过实例化HttpGet类创建 HttpGet 对象。使用 setConfig() 方法将上一步中创建的config对象设置为此对象。
//Create the HttpGet request object
HttpGet httpGet = new HttpGet("/");
//Setting the config to the request
httpget.setConfig(config);
Step 9 - Execute the request
通过将HttpHost对象(目标)和请求(HttpGet)作为参数传递到 execute() 方法中,来执行请求。
HttpResponse httpResponse = httpclient.execute(targetHost, httpget);
Example
下面的示例展示了如何使用用户名和密码通过代理执行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());
}
}