不推荐使用SocketFactory类型

时间:2014-03-13 08:45:13

标签: java android httpclient

我的申请进入绩效考核阶段。因此,Performs团队告诉我,有一些弃用的方法可用,写下最新的经文。

我的部分代码是:

  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));
}

此处SocketFactory ,LayeredSocketFactory,SSLSocketFactory,HttpProtocolParams ...

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

另一个示例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

请指导我。我正在使用HttpClient 4.3.jar文件。

请告诉我任何想法......

1 个答案:

答案 0 :(得分:1)

不推荐使用HttpClient类型的方法getParams()

在4.3.2之前,您可以使用getParams()方法(现在不建议使用)将参数设置到客户端,在4.3.2之后,您可以使用{{RequestConfig类通过Builder类设置请求参数1}}

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

然后只设置为HttpMethod(不像以前那样设置客户端)

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

不推荐使用HttpProtocolParams类型的方法setUserAgent(HttpParams,String)

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

不推荐使用HttpProtocolParams类型的方法setContentCharset(HttpParams,String)

使用AbstractHttpEntity子类(BasicHttpEntity,ByteArrayEntity,EntityTemplate,FileEntity,InputStreamEntity,SerializableEntity,StringEntity)

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

不推荐使用HttpProtocolParams类型

(4.3)使用“org.apache.http.config”和“org.apache.http.client.config

提供的配置类
Builder requestConfigBuilder = RequestConfig.custom();

不推荐使用SocketFactory类型&不推荐使用LayeredSocketFactory类型

SSLContext sslcontext = SSLContexts.createSystemDefault();

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

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

然后

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

您可以查看HttpClient Tutorial (4.3.x)了解更多示例和解释

我希望这会对你有所帮助。