Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The following code disables SSL certificate checking for any new instances of HttpsUrlConnection
/**
* Disables the SSL certificate checking for new instances of {@link HttpsURLConnection} This has been created to
* aid testing on a local box, not for use on production.
*/
private static void disableSSLCertificateChecking() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// Not implemented
}
} };
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
@sanketpatel2912
Copy link

sanketpatel2912 commented Aug 8, 2014

Thanks aembleton, I just call it once in my App started working fine.

@matoelorriaga
Copy link

matoelorriaga commented Oct 19, 2014

thanks!

@dwarakgovindp
Copy link

dwarakgovindp commented Mar 21, 2015

It worked like a charm !! So guys who are looking for a java equivalent for cURL command and bypassing the certificate validation for https using -k flag this is yours !!

@naveen7r
Copy link

naveen7r commented Apr 24, 2015

Is it working for android lollipop?

@sitanshu-zymr
Copy link

sitanshu-zymr commented Jul 28, 2015

Yes, It set to the HttpsURLConnection property. And It will effect in the context of the app.

@hunMyrte
Copy link

hunMyrte commented Jul 8, 2016

Thanks!
I added some lines and worked under android 6.
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } });
And It's very important that getAcceptedIssuers() has to return empty array instead of null.

@baiju-emedsim
Copy link

baiju-emedsim commented Jul 28, 2016

Thanks. Its work like a charm....!!!

@bferreirainfo
Copy link

bferreirainfo commented Nov 20, 2016

where doo put this code?i cant finde HttpURLConnection

@pandeyshivang
Copy link

pandeyshivang commented Apr 16, 2017

how to use this code in httpurlconncetion?

@jackie-d
Copy link

jackie-d commented Jun 23, 2017

How to re-enable it?

@ArabAgile
Copy link

ArabAgile commented Oct 9, 2017

What is the default ? disables SSL or not?

@seco35
Copy link

seco35 commented Sep 25, 2018

Is there anyone who had used that in react native and could give hints how to accomplish that?

@HEMANT8712
Copy link

HEMANT8712 commented Dec 8, 2018

I am working in react-native, how to incorporate this section of code and in which file as I am new to Java too.

@namnm
Copy link

namnm commented Mar 1, 2019

Still need this on react-native too! Any idea?

@Seeratabbaskhan
Copy link

Seeratabbaskhan commented Dec 16, 2019

Its not working fine

@MatheusCbrl
Copy link

MatheusCbrl commented Feb 18, 2020

witch document should I put this code?

@Mr-Ramzan
Copy link

Mr-Ramzan commented Mar 16, 2020

Thanks a lot!

@kimberly96
Copy link

kimberly96 commented Apr 16, 2020

in which file should i paste that code?

@colmulhall
Copy link

colmulhall commented Dec 7, 2021

Great, thanks!

@kaushikipandey
Copy link

kaushikipandey commented Jun 1, 2022

Where to copy paste this code?

@kaushikipandey
Copy link

kaushikipandey commented Jun 1, 2022

Great, thanks!

Where to copy paste this code??

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