我正在我自己实现的 java web 代理(将请求转发到实际的 web 服务)和一个 android 应用程序之间开发一个安全的 web 服务。
使用标准(不安全)http 连接这样做非常有效。现在我想在代理和安卓客户端之间使用安全(SSL)连接。
只要我为每个请求实例化一个新的 HttpClient ,这就会起作用,除了浪费资源非常慢,因为我正在为每个请求进行双向握手。
因此,我尝试为每个请求重用 HttpClient,这会导致以下异常中的安全连接
java.lang.IllegalStateException:连接已经打开。在 org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:150) 在 org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 在 org.apache.http.impl .client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient .java:487) 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
当我将代理和客户端更改为无 SSL 通信时,它可以正常工作。有人知道我做错了什么吗?非常感谢你的帮助!
顺便说一句,除了使用带有加载证书的 SSLServerSocket 而不是使用 ServerSocket 之外,服务器代码完全相似。
客户端参数设置
// Set basic data
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");
// Make pool
ConnPerRoute connPerRoute = new ConnPerRouteBean(12);
ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
ConnManagerParams.setMaxTotalConnections(params, 20);
// Set timeout
HttpConnectionParams.setStaleCheckingEnabled(params, false);
HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);
HttpConnectionParams.setSocketBufferSize(params, 8192);
// Some client params
HttpClientParams.setRedirecting(params, false);
Http客户端创建
// load truststore certificate
InputStream clientTruststoreIs = context.getResources().openRawResource(R.raw.server);
KeyStore trustStore = KeyStore.getInstance("BKS");
trustStore.load(clientTruststoreIs, "server".toCharArray());
// initialize trust manager factory with the read truststore
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
// setup client certificate
// load client certificate
InputStream keyStoreStream = context.getResources().openRawResource(R.raw.client);
KeyStore keyStore = KeyStore.getInstance("BKS");
keyStore.load(keyStoreStream, "client".toCharArray());
// initialize key manager factory with the read client
// certificate
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "client".toCharArray());
// initialize SSLSocketFactory to use the certificates
SSLSocketFactory socketFactory = new SSLSocketFactory(SSLSocketFactory.TLS, keyStore, "client", trustStore, null, null);
// Register http/s shemas!
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();