I am developing a secure web service between my self-service Java web proxy (which sends requests to the actual web service) and the android application.
Performing this standard (insecure) http connection works fine. Now I want to use a secure (SSL) connection between the proxy and the android client.
This works as long as I create a new HttpClient for each request, which is associated with an excessive waste of resources, as I do a two-way handshake for each request.
So, I'm trying to reuse HttpClient for every request that results in secure connections in the next exception.
java.lang.IllegalStateException: The connection is already open. at org.apache.http.impl.conn.AbstractPoolEntry.open (AbstractPoolEntry.java:150) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open (AbstractPooledConnAdapter.java:119) at org.apache.http.impl .client.DefaultRequestDirector.execute (DefaultRequestDirector.javahaps60) at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java∗55) at org.apache.http.impl.client.AbstractHttpClientexececient .java: 487) at org.apache.http.impl.client.AbstractHttpClient.execute (AbstractHttpClient.java:465)
When I change my proxy server and client to no-ssl, it works without problems. Does anyone know what I'm doing wrong? Many thanks for your help!
, , SSLServerSocket ServerSocket.
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams.setUserAgent(params, "Android app/1.0.0");
ConnPerRoute connPerRoute = new ConnPerRouteBean(12);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
ConnManagerParams.setMaxTotalConnections(params, 20);
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);
HttpConnectionParams.setSocketBufferSize(params, 8192);
HttpClientParams.setRedirecting(params, false);
Http
InputStream clientTruststoreIs = context.getResources().openRawResource(R.raw.server);
KeyStore trustStore = KeyStore.getInstance("BKS");
trustStore.load(clientTruststoreIs, "server".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
InputStream keyStoreStream = context.getResources().openRawResource(R.raw.client);
KeyStore keyStore = KeyStore.getInstance("BKS");
keyStore.load(keyStoreStream, "client".toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "client".toCharArray());
SSLSocketFactory socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, keyStore, "client", trustStore, null, null);
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("https", socketFactory, port));
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
httpClient = new DefaultHttpClient(conMgr, params);
HttpResponse response = httpClient.execute(request);
HttpEntity entity = response.getEntity();
in = entity.getContent();
String result = convertStreamToString(in);
in.close();