Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
private static OkHttpClient getUnsafeOkHttpClient() {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public void checkClientTrusted([] chain, String authType) throws CertificateException {
public void checkServerTrusted([] chain, String authType) throws CertificateException {
public[] getAcceptedIssuers() {
return null;
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new;
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
return okHttpClient;
} catch (Exception e) {
throw new RuntimeException(e);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment