Java 已弃用的HttpClient,现在使用apache-HttpClient-4.3.x
我有一些工作正常的代码,但现在已经被弃用了。除Java 已弃用的HttpClient,现在使用apache-HttpClient-4.3.x,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我有一些工作正常的代码,但现在已经被弃用了。除BasicCredentialsProvider外,所有部件均已除油 SSLSocketFactory sslsf = new SSLSocketFactory("TLS", null, null, keyStore, null, new TrustSelfSignedStrategy(), new AllowAllHostnameVerifier()); Scheme https = new Sche
BasicCredentialsProvider外,所有部件均已除油
SSLSocketFactory sslsf = new SSLSocketFactory("TLS", null, null, keyStore, null, new TrustSelfSignedStrategy(),
new AllowAllHostnameVerifier());
Scheme https = new Scheme("https", 28181, sslsf);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schemeRegistry.register(https);
PoolingClientConnectionManager connectionManager = new PoolingClientConnectionManager(schemeRegistry);
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(5);
DefaultHttpClient httpClient = new DefaultHttpClient(connectionManager);
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort(), name),
new UsernamePasswordCredentials(username, password));
httpClient.setCredentialsProvider(credsProvider);
httpClient.getParams().setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
return new HttpContextRequestScopedApacheClientExecutor(httpClient);
我已经试着自己做了。首先,我将SSLSocketFactory
替换为
SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory
(SSLContexts.custom().
loadTrustMaterial(keyStore, new TrustSelfSignedStrategy()).
useTLS().build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
然后我试着用
Registry Registry=RegistryBuilder.create()
.register(“https”,sslConnectionFactory)
.build()代码>
我无法适应我的港口那里,所以我在寻找了相当长的一段时间后,我仍然迷失在如何做到这一点。剩下的我还没有解决办法。这方面的任何帮助都是必要的。我想我已经设法做到了,到目前为止它还在工作
HttpClientBuilder builder = HttpClientBuilder.create();
KeyStore keyStore = initSSL();
SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(keyStore, new
TrustSelfSignedStrategy()).useTLS().build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", sslConnectionFactory)
.build();
PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(registry);
ccm.setMaxTotal(100);
ccm.setDefaultMaxPerRoute(5);
builder.setConnectionManager(ccm);
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort(), name),
new UsernamePasswordCredentials(username, password));
builder.setDefaultCredentialsProvider(credsProvider);
RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setSocketTimeout(60000);
builder.setDefaultRequestConfig(requestBuilder.build());
return new HttpContextRequestScopedApacheClientExecutor(builder.build());
HttpClientBuilder=HttpClientBuilder.create();
KeyStore KeyStore=initSSL();
SSLConnectionSocketFactory sslConnectionFactory=新的SSLConnectionSocketFactory(SSLContexts.custom().loadTrustMaterial(密钥库,新
TrustSelfSignedStrategy()).useTLS().build(),SSLConnectionSocketFactory.ALLOW_ALL_主机名_验证器);
Registry Registry=RegistryBuilder.create()
.register(“http”,PlainConnectionSocketFactory.getSocketFactory())
.register(“https”,sslConnectionFactory)
.build();
PoolightPClientConnectionManager ccm=新的PoolightPClientConnectionManager(注册表);
ccm.setMaxTotal(100);
ccm.setDefaultMaxPerRoute(5);
builder.setConnectionManager(ccm);
BasicCredentialsProvider credsProvider=新的BasicCredentialsProvider();
credsProvider.setCredentials(新的AuthScope(uri.getHost(),uri.getPort(),名称),
新用户名密码凭据(用户名、密码));
builder.setDefaultCredentialsProvider(CredProvider);
RequestConfig.Builder requestBuilder=RequestConfig.custom();
requestBuilder.setSocketTimeout(60000);
setDefaultRequestConfig(requestBuilder.build());
返回新的HttpContextRequestScopeDapCacheClientexecutor(builder.build());