Skip to content

Instantly share code, notes, and snippets.

@zjor
Created May 30, 2013 11:03
Show Gist options
  • Save zjor/5677136 to your computer and use it in GitHub Desktop.
Save zjor/5677136 to your computer and use it in GitHub Desktop.
How to avoid "Exception : javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated" Compliant with Apache Http Client 4.2.3
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
DefaultHttpClient base = new DefaultHttpClient();
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = base.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
DefaultHttpClient httpClient = new DefaultHttpClient(ccm, base.getParams());
@saurabh-agrawal83
Copy link

Thank you so much for this gist!!!

@liu418791536
Copy link

public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[] {}; }
Dose my code will recurrented this Exception ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment