在尝试了很多不同的事情后,我发现了一个解决问题的方法。不幸的是,我无法弄清楚如何禁用连接池。如果有人知道该怎么做,我想听听解决方案。
我最终创建了自己的自定义 SSL 套接字工厂,它充当默认 SSL 套接字工厂的传递,我在 HttpsURLConnection 类上设置了该工厂:
SSLContext context = SSLContext.getInstance("TLS");
context.init(new KeyManager[] { new ClientKeyManager() },
new TrustManager[] { new ClientTrustManager() },
new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(new ManagedSSLSocketFactory(context.getSocketFactory()));
请注意,我从 SSLContext 类传入了默认套接字工厂。ManagedSSLSocketFactory 的代码如下所示:
private class ManagedSSLSocketFactory extends SSLSocketFactory
{
private SSLSocketFactory m_socketFactory;
public ManagedSSLSocketFactory(SSLSocketFactory socketFactory)
{
m_socketFactory = socketFactory;
}
@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException
{
Socket socket = m_socketFactory.createSocket(s, host, port, autoClose);
//Note that m_arcThread, m_arcSocket, etc are defined in the containing class
if(Thread.currentThread() == m_arcThread)
{
if(m_arcSocketInvalid || (socket == m_ssrcSocket))
{
closeSocketQuietly(socket);
return createSocket(s, host, port, autoClose);
}
m_arcSocketInvalid = false;
}
... omitted some convoluted code that deals with edge cases
return socket;
}
... rest of implementation
}