4

I wanna trust all SSL certificates in my application.

I add these code in my android.app.Application.

try {
    TrustManager[] trustAllCerts = new TrustManager[] {
        new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                X509Certificate[] myTrustedAnchors = new X509Certificate[0];
                return myTrustedAnchors;
            }
        }
    };

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    });

} catch (Exception e) {
    Log.e(TAG, "nuke", e);
}

But volleyError.networkResponse.statusCode is always 500.

I go to JSONDataUrl with firefox, it works fine.

Help!

String JSONDataUrl = "https://something";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, JSONDataUrl, null,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            dismissDialog();
            Log.d(TAG, "response: " + response);
            }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            dismissDialog();
            if (DEBUG) {
                if (volleyError.networkResponse != null) {
                    Log.e(TAG, "headers: " + volleyError.networkResponse.headers);
                    Log.e(TAG, "statusCode: " + volleyError.networkResponse.statusCode);
                }
            }
            Log.e(TAG, volleyError.toString(), volleyError);
        }
    });
    requestQueue.add(jsonObjectRequest);
jww
  • 97,681
  • 90
  • 411
  • 885
android_su
  • 1,637
  • 4
  • 21
  • 30
  • 1
    Possible duplicate of [Trusting all certificates using HttpClient over HTTPS](http://stackoverflow.com/questions/2642777/trusting-all-certificates-using-httpclient-over-https). I think Emmby has the best answer, but it does not include trusting all certificates. Others provide code for that. – jww Aug 25 '14 at 09:51
  • I read that topic and Daniel's answer. But I don't find method SSLSocketFactory.setHostnameVerifier and SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER. – android_su Aug 25 '14 at 10:36
  • *"But volleyError.networkResponse.statusCode is always 500"* - that's a server error. You need to fix the server first. Once the server is fixed, you can move on to how the client connects to it. – jww Aug 25 '14 at 10:39
  • @jww I write code like Daniel's answer. It works fine now. Thanks so much. – android_su Sep 04 '14 at 09:41
  • @android_su: Could you please post your answer, how you have implemented in Volley – Manu Jul 02 '15 at 11:37
  • Not a good a idea, to trust on all certificates... – ivanleoncz Sep 15 '16 at 20:12

0 Answers0