Невозможно разрешить метод «setDefaultsocketCastory» в «PoolinghttpclientConnectionManager»JAVA

Программисты JAVA общаются здесь
Anonymous
Невозможно разрешить метод «setDefaultsocketCastory» в «PoolinghttpclientConnectionManager»

Сообщение Anonymous »

Я должен мигрировать этот код HttpClient 4 в HttpClient 5: < /p>

Код: Выделить всё

import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;

@Bean(name = "sslRestTemplateBean")
public RestTemplate sslRestTemplate() throws Exception {
char[] unlockPhrase = "test".toCharArray();
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(pkcsKeyStore("/key.p12", unlockPhrase), unlockPhrase)
.build();
HttpClient client = HttpClients.custom()
.setSSLContext(sslContext)
.build();
return new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
}

private KeyStore pkcsKeyStore(String file, char... password) throws Exception {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(file)) {
keyStore.load(in, password);
}
return keyStore;
}

static RestTemplate createSslRestTemplate(SslCertificate sslCertificate) throws Exception {
char[] password = generateRandomString().toCharArray();
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(keyStore(sslCertificate, password), password)
.build();
HttpClient client = HttpClients.custom()
.disableAutomaticRetries()
.setSSLContext(sslContext)
.build();
return new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
}
< /code>
Я перешел на это: < /p>
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.ssl.SSLContextBuilder;

@Bean(name = "sslRestTemplateBean")
public RestTemplate sslRestTemplate() throws Exception {
char[] unlockPhrase = "test".toCharArray();
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(pkcsKeyStore("/key.p12", unlockPhrase), unlockPhrase)
.build();

// Create SSL socket factory
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);

// Create connection manager with SSL socket factory
PoolingHttpClientConnectionManager connectionManager =
new PoolingHttpClientConnectionManager();
connectionManager.setDefaultSocketFactory(sslSocketFactory);

// Build HTTP client
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(connectionManager)
.build();

return new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
}

private KeyStore pkcsKeyStore(String file, char... password) throws Exception {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(file)) {
keyStore.load(in, password);
}
return keyStore;
}

static RestTemplate createSslRestTemplate(SslCertificate sslCertificate) throws Exception {
char[] password = generateRandomString().toCharArray();
SSLContext sslContext = SSLContextBuilder.create()
.loadKeyMaterial(keyStore(sslCertificate, password), password)
.build();

// Create SSL socket factory
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);

// Create connection manager with SSL socket factory
PoolingHttpClientConnectionManager connectionManager =
new PoolingHttpClientConnectionManager();
connectionManager.setDefaultSocketFactory(sslSocketFactory);

// Build HTTP client
CloseableHttpClient client = HttpClients.custom()
.disableAutomaticRetries()
.setConnectionManager(connectionManager)
.build();

return new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
}
Но я получаю ошибку: не могу разрешить метод 'setdefaultsockectectory' в 'boolinghttpclientconnectionmanager'
Вы знаете, как я могу решить эту проблему и правильно перенести код?>

Подробнее здесь: https://stackoverflow.com/questions/797 ... onnectionm

Вернуться в «JAVA»