use of org.apache.hc.core5.http.protocol.RequestConnControl in project californium by eclipse.
the class HttpClientFactory method createClient.
/**
* Create the pooled asynchronous http client.
*
* @param config configuration for the http client
* @return asynchronous http client
* @since 3.0 (changed parameter to Configuration)
*/
public static CloseableHttpAsyncClient createClient(Configuration config) {
int connectionIdleSecs = config.getTimeAsInt(Proxy2Config.HTTP_CONNECTION_IDLE_TIMEOUT, TimeUnit.SECONDS);
final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().disableCookieManagement().setDefaultRequestConfig(createCustomRequestConfig(config)).setConnectionManager(createPoolingConnManager(config)).setVersionPolicy(HttpVersionPolicy.NEGOTIATE).setIOReactorConfig(IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(connectionIdleSecs)).build()).addRequestInterceptorFirst(new RequestConnControl()).addRequestInterceptorFirst(new RequestDate()).addRequestInterceptorFirst(new RequestExpectContinue()).addRequestInterceptorFirst(new RequestTargetHost()).addRequestInterceptorFirst(new RequestUserAgent()).setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
@Override
public TimeValue getKeepAliveDuration(HttpResponse response, HttpContext context) {
TimeValue keepAlive = super.getKeepAliveDuration(response, context);
if (keepAlive == null || keepAlive.getDuration() < 0) {
// Keep connections alive if a keep-alive value
// has not be explicitly set by the server
keepAlive = KEEP_ALIVE;
}
return keepAlive;
}
}).build();
client.start();
return client;
}
use of org.apache.hc.core5.http.protocol.RequestConnControl in project httpcomponents-core by apache.
the class ClassicIntegrationTest method testAbsentHostHeader.
@Test
public void testAbsentHostHeader() throws Exception {
// Initialize the server-side request handler
this.server.registerHandler("*", (request, response, context) -> response.setEntity(new StringEntity("All is well", StandardCharsets.US_ASCII)));
this.server.start();
this.client.start(new DefaultHttpProcessor(RequestContent.INSTANCE, new RequestConnControl()));
final HttpCoreContext context = HttpCoreContext.create();
final HttpHost host = new HttpHost(scheme.id, "localhost", this.server.getPort());
final BasicClassicHttpRequest get1 = new BasicClassicHttpRequest(Method.GET, "/");
get1.setVersion(HttpVersion.HTTP_1_0);
try (final ClassicHttpResponse response1 = this.client.execute(host, get1, context)) {
Assertions.assertEquals(200, response1.getCode());
EntityUtils.consume(response1.getEntity());
}
final BasicClassicHttpRequest get2 = new BasicClassicHttpRequest(Method.GET, "/");
try (final ClassicHttpResponse response2 = this.client.execute(host, get2, context)) {
Assertions.assertEquals(400, response2.getCode());
EntityUtils.consume(response2.getEntity());
}
}
Aggregations