自定义HTTP客户端配置(Java)

如果运行SDK的应用程序需要自定义HTTP客户端,要启用配置SSL或向请求添加默认标头等功能,则需要使用ClientConfig.builder().httpClient()配置TargetClient

基本自定义HTTP客户端配置

SDK当前支持实施org.apache.http.client.HttpClient接口的HTTP客户端。

基本实施

CloseableHttpClient httpClient = HttpClients.custom().build();
ClientConfig clientConfig = ClientConfig.builder()
    .client("acmeclient")
    .organizationId("1234567890@AdobeOrg")
    .httpClient(httpClient)
    .build();
TargetClient targetClient = TargetClient.create(clientConfig);

使用SSL配置进行自定义HTTP客户端配置

以下是如何通过自定义传递到ClientConfigHttpClientTargetClient中配置SSL的示例。 以下代码片段使用org.apache.http.conn.ssl包中的类进行SSL配置。

SSL实施

SSLContext context = SSLContextBuilder.create().build();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(context);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
ClientConfig clientConfig = ClientConfig.builder()
    .client("acmeclient")
    .organizationId("1234567890@AdobeOrg")
    .httpClient(httpClient)
    .build();
TargetClient targetClient = TargetClient.create(clientConfig);
recommendation-more-help
6906415f-169c-422b-89d3-7118e147c4e3