gpt4 book ai didi

java - 类型 SocketFactory 已弃用

转载 作者:行者123 更新时间:2023-11-30 03:02:05 26 4
gpt4 key购买 nike

我的申请进入绩效审核阶段。所以 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

另一个示例已弃用

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 文件。

请告诉我任何想法...

最佳答案

HttpClient 类型的 getParams() 方法已弃用

在 4.3.2 之前,您可以使用 getParams() 方法(现已弃用)向客户端设置参数,在 4.3.2 之后,您可以通过 RequestConfig< 设置请求参数 使用 Builder

的类
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)更多示例和解释

希望对你有帮助

关于java - 类型 SocketFactory 已弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22373285/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com