android okhttp有预连接这种操作么提前ssl握手
Yes, Android OkHttp supports connection pooling and pre-connecting to a host to reduce the overhead of SSL/TLS handshakes. This is achieved through the use of the ConnectionPool and ConnectionSpec classes.
To enable connection pooling, you can create a new ConnectionPool instance and set it as the connection pool for your OkHttpClient:
ConnectionPool connectionPool = new ConnectionPool();
OkHttpClient okHttpClient = new OkHttpClient.Builder().connectionPool(connectionPool).build();
By default, connection pooling is enabled and the maximum number of idle connections per address is set to 5.
To pre-connect to a host, you can use the ConnectionSpec class to specify the SSL/TLS configuration for the connection and create a new Connection instance:
ConnectionSpec connectionSpec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS).build();
Address address = new Address(host, port);
Connection connection = new Connection.Builder().address(address).connectionSpecs(Collections.singletonList(connectionSpec)).build();
You can then use this Connection instance to perform requests:
Request request = new Request.Builder().url(url).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Handle failure
}
@Override
public void onResponse(Call call, Response response) throws IOException {
// Handle response
}
});
``
原文地址: https://www.cveoy.top/t/topic/eZic 著作权归作者所有。请勿转载和采集!