Certified Self-signed Volley Android

1

I have the following method that makes a call to a service by volley

public GenericController(boolean flag) {


   if(flag) {
       objectRequest = new ObjetRequest(1, "", BaseRequest.class, "", new Response.Listener<BaseRequest>() {
           @Override
           public void onResponse(BaseRequest response) {

               // response
           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               // Error
           }
       }) {
           @Override
           public Map<String, String> getHeaders() throws AuthFailureError {
               Map<String, String> params = new HashMap<String, String>();
               params.put("sessionToken", Globals.getSessionToken());
               params.put("Content-Type", "application/json; charset=utf-8");

               return params;
           }
       };
   }
    else {
       objectRequest = new ObjetRequest<>(1, "", BaseRequest.class, "", new Response.Listener<BaseRequest>() {
           @Override
           public void onResponse(BaseRequest response) {

               // response
           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               // Error
           }
       });

   }
}

What do you use:

 public void servicio_Get_Bancos(final Context context, String request, final Response.Listener<Get_Bancos_IFIS> listener, final Response.ErrorListener errorListener) {
    int method = Request.Method.POST;
    String serviceURL = URLLocal;
    String req = "";
    try {
        req = request;
        RequestQueue queue = Volley.newRequestQueue(context, null);
        objectRequest = new ObjetRequest(method, serviceURL, Get_Bancos_IFIS.class, req, listener, errorListener);
        objectRequest.setRetryPolicy(new DefaultRetryPolicy(TIME_OUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(objectRequest);
    } catch (Exception e) {
        Logger logger = Logger.getAnonymousLogger();
        logger.log(Level.SEVERE, "an exception was thrown", e);
    }
}

The problem is that the url I'm pointing to has a self-signed certificate, and when trying to use the service, it always goes with the error listener and in the logcat does not see error message, some way to ignore the certificates or solve this issue?

    
asked by Bruno Sosa Fast Tag 10.01.2018 в 17:33
source

1 answer

1

Resolvi placing this Class to disable certificates

public  class NukeSSLCerts {
protected static final String TAG = "NukeSSLCerts";

public static void nuke() {
    try {
        TrustManager[] trustAllCerts = new TrustManager[] {
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        X509Certificate[] myTrustedAnchors = new X509Certificate[0];
                        return myTrustedAnchors;
                    }

                    @Override
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {}

                    @Override
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {}
                }
        };

        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) {
    }
}

}

Should be used only for development, never for Production

In the class where the service is only instancie a class of these and I call the method nuke()

    
answered by 11.01.2018 / 21:41
source