Java
- 1 ответ
- 0 вопросов
0
Вклад в тег
private static DefaultHttpClient getThreadSafeClientHTTPS(Context context)
throws KeyManagementException, UnrecoverableKeyException, NoSuchAlgorithmException,
KeyStoreException, java.security.cert.CertificateException, FileNotFoundException {
KeyStore trustStore = KeyStore.getInstance("BKS");
AssetManager assetManager = context.getAssets();
InputStream instream = null;
try {
instream = assetManager.open("PATH TO CERT");
} catch (IOException e1) {
e1.printStackTrace();
}
try {
// password
trustStore.load(instream, PASSWORD HERE);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
instream.close();
} catch (Exception ignore) {
}
}
// Create socket factory with given keystore.
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme sch = new Scheme("https", socketFactory, 443);
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
DefaultHttpClient client = new DefaultHttpClient(params);
ClientConnectionManager mgr = client.getConnectionManager();
params = client.getParams();
int timeoutConnection = 10 * 1000;
HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 20 * 1000;
HttpConnectionParams.setSoTimeout(params, timeoutSocket);
client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, mgr.getSchemeRegistry()),
params);
client.getConnectionManager().getSchemeRegistry().register(sch);
return client;
}