/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* .
*
*/
package com.ogosecurity.proxy.http2samples;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.async.HttpAsyncClients;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.ClientTlsStrategyBuilder;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.nio.AsyncEntityProducer;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
import org.apache.hc.core5.io.CloseMode;
/**
* Class using new HttpClient 5.0-beta6 Async APIs will work with a website with same IP address than destServerNameIP,
* will fail in other case.
*
* Just change the USE_WEBSITE_WITH_IP_DIFFERENT_THAN_INNET_ADDRESS from true to false to test two real examples.
*
* @author Nicolas de Pomereu
*
*/
public class RawSSLAsyncRegularTest {
private static boolean USE_WEBSITE_WITH_IP_DIFFERENT_THAN_INNET_ADDRESS = true;
public final static void main(final String[] args) throws Exception {
System.out.println(new Date() + " Starting...");
final TlsStrategy tlsStrategy = ClientTlsStrategyBuilder.create()
.build();
PoolingAsyncClientConnectionManager cm = PoolingAsyncClientConnectionManagerBuilder.create()
.setTlsStrategy(tlsStrategy)
.build();
try (final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
.setConnectionManager(cm).build()) {
client.start();
String content = execute(client);
System.out.println(content);
client.close(CloseMode.GRACEFUL);
}
System.out.println(new Date() + " Done.");
}
private static String execute(final CloseableHttpAsyncClient client)
throws InterruptedException, ExecutionException, IOException {
OutputStream out = new ByteArrayOutputStream();
String initHost = null;
String destServerNameIP = "185.74.10.195";
String destScheme = "https";
int port = 443;
if (USE_WEBSITE_WITH_IP_DIFFERENT_THAN_INNET_ADDRESS ) {
initHost = "www.kremote-files.com"; // Will fail with this class Async APIs (is OK with Classic APIs).
}
else {
initHost = "www.aceql.com"; // Will work OK.
}
InetAddress inetAddress = InetAddress.getByName(destServerNameIP);
final HttpHost target = new HttpHost(destScheme, inetAddress, initHost, port);
final String requestUri = "/";
final AsyncEntityProducer entityProducer = null;
SimpleHttpRequest simpleHttpRequest = new SimpleHttpRequest("GET", target, requestUri);
BasicRequestProducer basicRequestProducer = new BasicRequestProducer("GET", target, requestUri, entityProducer);
SimpleRequestProducer.create(simpleHttpRequest);
final Future future = client.execute(basicRequestProducer, new OutputStreamBinResponseConsumer(out),
null);
future.get();
return out.toString();
}
}