📅  最后修改于: 2020-11-18 08:30:03             🧑  作者: Mango
在本章中,我们将使用一个示例,学习如何创建一个使用用户名和密码进行身份验证的HttpRequest,并将其通过代理隧道传输到目标主机。
CredentialsProvider接口维护一个集合来保存用户登录凭据。您可以通过实例化BasicCredentialsProvider类(此接口的默认实现)来创建其对象。
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
您可以使用setCredentials()方法将所需的凭据设置为CredentialsProvider对象。此方法接受两个对象-
AuthScope对象-身份验证范围,指定诸如主机名,端口号和身份验证方案名称之类的详细信息。
凭据对象-指定凭据(用户名,密码)。如下所示,使用setCredentials()方法为主机和代理设置凭据。
credsProvider.setCredentials(new AuthScope("example.com", 80), new
UsernamePasswordCredentials("user", "mypass"));
credsProvider.setCredentials(new AuthScope("localhost", 8000), new
UsernamePasswordCredentials("abc", "passwd"));
使用HttpClients类的custom()方法创建HttpClientBuilder ,如下所示-
//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();
您可以使用setDefaultCredentialsProvider()方法将CredentialsProvider对象设置为HttpClientBuilder对象。将先前创建的CredentialsProvider对象传递给此方法。
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
使用build()方法构建CloseableHttpClient对象。
CloseableHttpClient httpclient = clientbuilder.build();
通过实例化HttpHost类创建目标主机和代理主机。
//Creating the target and proxy hosts
HttpHost target = new HttpHost("example.com", 80, "http");
HttpHost proxy = new HttpHost("localhost", 8000, "http");
使用custom()方法创建一个RequestConfig.Builder对象。设置先前创建的对象的ProxyHost使用setProxy()方法RequestConfig.Builder。最后,使用build()方法构建RequestConfig对象。
RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
RequestConfig config = reqconfigconbuilder.build();
通过实例化HttpGet类来创建HttpGet对象。使用setConfig()方法将在上一步中创建的config对象设置为此对象。
//Create the HttpGet request object
HttpGet httpGet = new HttpGet("/");
//Setting the config to the request
httpget.setConfig(config);
通过将HttpHost对象(目标)和请求(HttpGet)作为参数传递给execute()方法来执行请求。
HttpResponse httpResponse = httpclient.execute(targetHost, httpget);
以下示例演示了如何使用用户名和密码通过代理执行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());
}
}
在执行时,以上程序生成以下输出-
HTTP/1.1 200 OK