Skip to content

Instantly share code, notes, and snippets.

@ricston-git
Created July 24, 2015 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ricston-git/0e7955a3b80ec58580fe to your computer and use it in GitHub Desktop.
Save ricston-git/0e7955a3b80ec58580fe to your computer and use it in GitHub Desktop.
public class CustomHttpClient extends DefaultHttpClient
{
final Context context;
public CustomHttpClient(Context context)
{
this.context = context;
}
@Override
protected ClientConnectionManager createClientConnectionManager()
{
try
{
String defaultType = KeyStore.getDefaultType();
KeyStore trustedStore = KeyStore.getInstance(defaultType);
InputStream certificateStream = context.getResources().openRawResource(R.raw.keystore);
trustedStore.load(certificateStream, "password".toCharArray());
certificateStream.close();
SSLSocketFactory sslSocketFactory = new SSLSocketFactory(trustedStore);
sslSocketFactory.setHostnameVerifier((X509HostnameVerifier) SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 8080));
registry.register(new Scheme("https", sslSocketFactory, 8443));
return new SingleClientConnManager(getParams(), registry);
}
catch (Exception e)
{
throw new AssertionError(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment