use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project tutorials by eugenp.
the class HttpAsyncClientLiveTest method whenUseAuthenticationWithHttpAsyncClient_thenCorrect.
@Test
public void whenUseAuthenticationWithHttpAsyncClient_thenCorrect() throws Exception {
final CredentialsProvider provider = new BasicCredentialsProvider();
final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS);
provider.setCredentials(AuthScope.ANY, creds);
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setDefaultCredentialsProvider(provider).build();
final HttpGet request = new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION);
client.start();
final Future<HttpResponse> future = client.execute(request, null);
final HttpResponse response = future.get();
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
client.close();
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project tutorials by eugenp.
the class HttpAsyncClientLiveTest method whenUseProxyWithHttpClient_thenCorrect.
@Test
public void whenUseProxyWithHttpClient_thenCorrect() throws Exception {
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
final HttpHost proxy = new HttpHost("127.0.0.1", 8080);
final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
final HttpGet request = new HttpGet(HOST_WITH_PROXY);
request.setConfig(config);
final Future<HttpResponse> future = client.execute(request, null);
final HttpResponse response = future.get();
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
client.close();
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project tutorials by eugenp.
the class HttpAsyncClientLiveTest method whenUseSSLWithHttpAsyncClient_thenCorrect.
@Test
public void whenUseSSLWithHttpAsyncClient_thenCorrect() throws Exception {
final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setSSLHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER).setSSLContext(sslContext).build();
client.start();
final HttpGet request = new HttpGet(HOST_WITH_SSL);
final Future<HttpResponse> future = client.execute(request, null);
final HttpResponse response = future.get();
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
client.close();
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project tutorials by eugenp.
the class HttpAsyncClientLiveTest method whenUseMultipleHttpAsyncClient_thenCorrect.
@Test
public void whenUseMultipleHttpAsyncClient_thenCorrect() throws Exception {
final ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
final PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setConnectionManager(cm).build();
client.start();
final String[] toGet = { "http://www.google.com/", "http://www.apache.org/", "http://www.bing.com/" };
final GetThread[] threads = new GetThread[toGet.length];
for (int i = 0; i < threads.length; i++) {
final HttpGet request = new HttpGet(toGet[i]);
threads[i] = new GetThread(client, request);
}
for (final GetThread thread : threads) {
thread.start();
}
for (final GetThread thread : threads) {
thread.join();
}
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project tutorials by eugenp.
the class HttpAsyncClientLiveTest method whenUseCookiesWithHttpAsyncClient_thenCorrect.
@Test
public void whenUseCookiesWithHttpAsyncClient_thenCorrect() throws Exception {
final BasicCookieStore cookieStore = new BasicCookieStore();
final BasicClientCookie cookie = new BasicClientCookie(COOKIE_NAME, "1234");
cookie.setDomain(COOKIE_DOMAIN);
cookie.setPath("/");
cookieStore.addCookie(cookie);
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
client.start();
final HttpGet request = new HttpGet(HOST_WITH_COOKIE);
final HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
final Future<HttpResponse> future = client.execute(request, localContext, null);
final HttpResponse response = future.get();
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
client.close();
}
Aggregations