user3391455
user3391455

Reputation: 51

Type SocketFactory is deprecated

My application goes to Performance review stage. So Performs team told me there are some deprecated methods are available, write latest versons.

My part of the code is :

  private void workAroundReverseDnsBugInHoneycombAndEarlier(HttpClient client) {
    // Android had a bug where HTTPS made reverse DNS lookups (fixed in Ice Cream Sandwich) 
    // http://code.google.com/p/android/issues/detail?id=13117
    SocketFactory socketFactory = new LayeredSocketFactory() {
        SSLSocketFactory delegate = SSLSocketFactory.getSocketFactory();
        @Override 
        public Socket createSocket() throws IOException {
            return delegate.createSocket();
        }
        @Override 
        public Socket connectSocket(Socket sock, String host, int port, InetAddress localAddress, int localPort, HttpParams params) throws IOException {
            return delegate.connectSocket(sock, host, port, localAddress, localPort, params);
        }
        @Override 
        public boolean isSecure(Socket sock) throws IllegalArgumentException {
            return delegate.isSecure(sock);
        }
        @Override 
        public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
            injectHostname(socket, host);
            return delegate.createSocket(socket, host, port, autoClose);
        }
        private void injectHostname(Socket socket, String host) {
            try {
                Field field = InetAddress.class.getDeclaredField("hostName");
                field.setAccessible(true);
                field.set(socket.getInetAddress(), host);
            } catch (Exception ignored) {
            }
        }
    };
    client.getConnectionManager().getSchemeRegistry().register(new Scheme("https", socketFactory, 443));
}

Here SocketFactory ,LayeredSocketFactory,SSLSocketFactory,HttpProtocolParams ... says

Multiple markers at this line
- The method getParams() from the type HttpClient is deprecated
- The method setUserAgent(HttpParams, String) from the type HttpProtocolParams is 
 deprecated
- The type HttpProtocolParams is deprecated
- The method getParams() from the type HttpClient is deprecated
- The method setContentCharset(HttpParams, String) from the type HttpProtocolParams is 
 deprecated
- The type HttpProtocolParams is deprecated

another sample deprecated

Multiple markers at this line
- The type SocketFactory is deprecated
- The type LayeredSocketFactory is deprecated
- The type SSLSocketFactory is deprecated
- The type SSLSocketFactory is deprecated
- The method getSocketFactory() from the type SSLSocketFactory is 
 deprecated
- The type SSLSocketFactory is deprecated

Please guide me . I am using HttpClient 4.3.jar file.

Please tell me any ideas...

Upvotes: 1

Views: 9189

Answers (1)

vzamanillo
vzamanillo

Reputation: 10534

The method getParams() from the type HttpClient is deprecated

Before 4.3.2 you could set the parameters to the client using the getParams() method (deprecated now), after 4.3.2 you can set the request params via the RequestConfig class using a Builder

Builder requestConfigBuilder = RequestConfig.custom();
requestConfigBuilder.setConnectionRequestTimeout(1000).setMaxRedirects(1);

and then set to the HttpMethod only (not to client like before)

HttpGet request = new HttpGet(builder.build());
request.setConfig(requestConfigBuilder.build());

The method setUserAgent(HttpParams, String) from the type HttpProtocolParams is deprecated

CloseableHttpClient httpclient = HttpClients.custom().setUserAgent("Agent").build();

The method setContentCharset(HttpParams, String) from the type HttpProtocolParams is deprecated

Use AbstractHttpEntity subclasses (BasicHttpEntity, ByteArrayEntity, EntityTemplate, FileEntity, InputStreamEntity, SerializableEntity, StringEntity)

HttpPost post = new HttpPost("http://");
post.setEntity(new StringEntity("content", "UTF-8"));

The type HttpProtocolParams is deprecated

(4.3) use configuration classes provided 'org.apache.http.config' and 'org.apache.http.client.config'

Builder requestConfigBuilder = RequestConfig.custom();

The type SocketFactory is deprecated & The type LayeredSocketFactory is deprecated

SSLContext sslcontext = SSLContexts.createSystemDefault();

// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
        sslcontext,
        new String[] { "TLSv1" },
        null,
        SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

or

SSLContext sslContext = SSLContexts.custom()
        .useTLS()
        .loadTrustMaterial(myTrustStore)
        .build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);

then

CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

You can take a look at HttpClient Tutorial (4.3.x) for more examples and explanations

I hope this helps you.

Upvotes: 1

Related Questions