我嘗試了很多不同的事情後,發現瞭解決我的問題的解決方案。不幸的是,我無法弄清楚如何禁用連接池。如果有人知道如何做到這一點,我想聽聽解決方案。
我結束了創建自己的自定義的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
}