Configure the URLConnection-based HTTP client - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Configure the URLConnection-based HTTP client

The Amazon SDK for Java 2.x offers a lighter-weight UrlConnectionHttpClient HTTP client in comparison to the default ApacheHttpClient. The UrlConnectionHttpClient is based on Java's URLConnection.

The UrlConnectionHttpClient loads more quickly than the Apache-based HTTP client, but has fewer features. Because it loads more quickly, it is a good solution for Java Amazon Lambda functions.

The UrlConnectionHttpClient has several configurable options that you can access.


The UrlConnectionHttpClient does not support the HTTP PATCH method.

A handful of Amazon API operations require PATCH requests. Those operation names usually start with Update*. The following are several examples.

If you might use the UrlConnectionHttpClient, first refer to the API Reference for the Amazon Web Service that you're using. Check to see if the operations you need use the PATCH operation.

Access the UrlConnectionHttpClient

To configure and use the UrlConnectionHttpClient, you declare a dependency on the url-connection-client Maven artifact in your pom.xml file.

Unlike the ApacheHttpClient, the UrlConnectionHttpClient is not automatically added to your project, so use must specifically declare it.

The following example of a pom.xml file shows the dependencies required to use and configure the HTTP client.

<dependencyManagement> <dependencies> <dependency> <groupId></groupId> <artifactId>bom</artifactId> <version>2.17.290</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- other dependencies such as s3 or dynamodb --> <dependencies> <dependency> <groupId></groupId> <artifactId>url-connection-client</artifactId> </dependency> </dependencies>

Use and configure the UrlConnectionHttpClient

You can configure an instance of UrlConnectionHttpClient along with building a service client, or you can configure a single instance to share across multiple service clients.

With either approach, you use the UrlConnectionHttpClient.Builder to configure the properties for the URLConnection-based HTTP client.

Best practice: dedicate an UrlConnectionHttpClient instance to a service client

If you need to configure an instance of the UrlConnectionHttpClient, we recommend that you build the dedicated UrlConnectionHttpClient instance. You can do so by using the httpClientBuilder method of the service client's builder. This way, the lifecycle of the HTTP client is managed by the SDK, which helps avoid potential memory leaks if the UrlConnectionHttpClient instance is not closed down when it's no longer needed.

The following example creates an S3Client and configures the embedded instance of UrlConnectionHttpClient with socketTimeout and proxyConfiguration values. The proxyConfiguration method takes a Java lambda expression of type Consumer<ProxyConfiguration.Builder>.


import; import; import; import java.time.Duration;


// Singleton: Use the s3Client for all requests. S3Client s3Client = S3Client.builder() .httpClientBuilder(UrlConnectionHttpClient.builder() .socketTimeout(Duration.ofMinutes(5)) .proxyConfiguration(proxy -> proxy.endpoint(URI.create("")))) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); // Perform work with the s3Client. s3Client.close(); // Requests completed: Close the s3client.

Alternative approach: share an UrlConnectionHttpClient instance

To help keep resource and memory usage lower for your application, you can configure an UrlConnectionHttpClient and share it across multiple service clients. The HTTP connection pool will be shared, which lowers resource usage.


When an UrlConnectionHttpClient instance is shared, you must close it when it is ready to be disposed. The SDK will not close the instance when the service client is closed.

The following example configures an URLConnection-based HTTP client that is used by two service clients. The configured UrlConnectionHttpClient instance is passed to the httpClient method of each builder. When the service clients and the HTTP client are no longer needed, the code explicitly closes them. The code closes the HTTP client last.


import; import; import; import; import; import; import; import; import java.time.Duration;


SdkHttpClient urlHttpClient = UrlConnectionHttpClient.create(); // Singletons: Use the s3Client and dynamoDbClient for all requests. S3Client s3Client = S3Client.builder() .httpClient(urlHttpClient) .defaultsMode(DefaultsMode.IN_REGION) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); DynamoDbClient dynamoDbClient = DynamoDbClient.builder() .httpClient(urlHttpClient) .defaultsMode(DefaultsMode.IN_REGION) .credentialsProvider(EnvironmentVariableCredentialsProvider.create()) .build(); // Perform work with the s3Client and dynamoDbClient. // Requests completed: Close all service clients. s3Client.close(); dynamoDbClient.close(); urlHttpClient.close();

When you use the UrlConnectionHttpClient in your application, you must supply each service client with either a URLConnectionHttpClient instance or a ApacheHttpClient instance using the service client builder's httpClientBuilder method.

An exception occurs if your program uses multiple service clients and both of the following are true:

  • One service client is configured to use a UrlConnectionHttpClient instance

  • Another service client uses the default ApacheHttpClient without explicitly building it with the httpClient() or httpClientBuilder() methods

The exception will state that multiple HTTP implementations were found on the classpath.

The following example code snippet leads to an exception.

// The dynamoDbClient uses the UrlConnectionHttpClient DynamoDbClient dynamoDbClient = DynamoDbClient.builder() .httpClient(UrlConnectionHttpClient.create()) .build(); // The s3Client below uses the ApacheHttpClient at runtime, without specifying it. // An SdkClientException is thrown with the message that multiple HTTP implementations were found on the classpath. S3Client s3Client = S3Client.create(); // Perform work with the s3Client and dynamoDbClient. dynamoDbClient.close(); s3Client.close();

Avoid the exception by explicitly configuring the S3Client with an ApacheHttpClient.

DynamoDbClient dynamoDbClient = DynamoDbClient.builder() .httpClient(UrlConnectionHttpClient.create()) .build(); S3Client s3Client = S3Client.builder() .httpClient(ApacheHttpClient.create()) // Explicitly build the ApacheHttpClient. .build(); // Perform work with the s3Client and dynamoDbClient. dynamoDbClient.close(); s3Client.close();

To explicitly create the ApacheHttpClient, you must add a dependency on the apache-client artifact in your Maven project file.

Proxy configuration example

The following code snippet uses the proxy configuration builder for the URL connection HTTP client.

SdkHttpClient urlHttpClient = UrlConnectionHttpClient.builder() .proxyConfiguration(ProxyConfiguration.builder() .endpoint(URI.create("")) .username("username") .password("password") .addNonProxyHost("localhost") .addNonProxyHost("") .build()) .build();

The equivalent Java system properties for the proxy configuration are shown in the following command line snippet.

$ java -Dhttp.proxyPort=1234 -Dhttp.proxyUser=username \ -Dhttp.proxyPassword=password -Dhttp.nonProxyHosts=localhost| -cp ... App

The equivalent setup that uses environment variables is:

// Set the following environment variables. // $ export HTTP_PROXY="" // $ export NO_PROXY="localhost|" // Set the 'useSystemPropertyValues' to false on the proxy configuration. SdkHttpClient apacheHttpClient = UrlConnectionHttpClient.builder() .proxyConfiguration(ProxyConfiguration.builder() .useSystemPropertyValues(Boolean.FALSE) .build()) .build(); // Run the application. // $ java -cp ... App

The URLConnection-based HTTP client does not currently support HTTPS proxy system properties or the HTTPS_PROXY environment variable.