Skip to main content

Use the HttpClient Accessor To Configure Requests To Remote Services

The SAP Cloud SDK offers basic functionality that helps with connecting to other systems and services like SAP S/4HANA Cloud or On-premise edition. The SAP Cloud SDK leverages the existing API of HttpClient and applies conveniently managed properties, e.g. according to a specific destination configuration. In the following paragraphs, the HttpClientAccessor API and its usage will be described.

General Usage

In general an HttpClient can be instantiated through the HttpClientAcessor. The SAP Cloud SDK itself uses the accessor class for all internal requests as well.

To make use the HttpClientAccessor, make sure to include the cloudplatform-connectivity dependency in your project.

HttpClient client = HttpClientAccessor.getHttpClient();

If you need an HttpClient to reach a system that has been configured as a Destination (e.g. an On-premise S/4HANA system), you may first fetch the destination and then use it as a argument for the accessor:

HttpDestination destination = DestinationAccessor.getDestination("my-destination").asHttp();
HttpClient client = HttpClientAccessor.getHttpClient(destination);

When using a destination, the configured destination URL will be used as base path for the subsequent requests for client.

note

Please note that similar to other accessor-based APIs, the SAP Cloud SDK offers methods with a try prefix to allow for optional VAVR-enhanced API access.

Customization

When the properties of HttpClient are not working for the application, e.g. timeout is too short or too long, then the generation can be customized. Please find the HttpClientFactory interface. They offer a similar method createHttpClient with optional destination argument:

HttpClientFactory factory = new DefaultHttpClientFactory();

HttpClient genericClient = factory.createHttpClient();
HttpClient destinationClient = factory.createHttpClient(destination);

The DefaultHttpClientFactory type offers a static builder, to enable custom properties for:

  • timeoutMilliseconds
  • maxConnectionsPerRoute
  • maxConnectionsTotal
HttpClientFactory customFactory = DefaultHttpClientFactory.builder()
.timeoutMilliseconds(60000)
.maxConnectionsPerRoute(100)
.maxConnectionsTotal(200)
.build();

When inheriting from DefaultHttpClientFactory it's possible to provide even deeper customization:

DefaultHttpClientFactory customFactory = new DefaultHttpClientFactory() {
@Override
protected RequestConfig.Builder getRequestConfigBuilder( HttpDestinationProperties destination ) {
return super.getRequestConfigBuilder(destination)
.setProxy(new HttpHost("proxy", 8080, "http"));
}
@Override
protected HttpClientBuilder getHttpClientBuilder( HttpDestinationProperties destination ) {
return super.getHttpClientBuilder(destination)
.setUserAgent("SDK");
}
};

It is possible to take advantage of calls to super - or use your own objects directly. This inheritance enables custom implementation for the following methods:

  • getHttpClientBuilder
  • getRequestConfigBuilder
  • getSocketConfigBuilder
  • getConnectionManager

Overriding Default Behavior

Now the customization of the HTTP client factory is available and we can adjust the default behavior for the accessor:

HttpClientFactory factory = new MyCustomHttpClientFactory();
HttpClientAccessor.setHttpClientFactory(factory);

From now on the custom factory will be used for every explicit and implicit HTTP request running through the SAP Cloud SDK.