Making a HTTPS request using Android Volley

后端 未结 10 1787
庸人自扰
庸人自扰 2020-11-28 19:34

I am trying to make a https request using this code:

RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
request = new Request

        
10条回答
  •  孤街浪徒
    2020-11-28 20:11

    If you are using volley and want to HTTPS request or SSL Certified service then you can choose this easiest way : -->

    Step --> 1. keep .cer file into res/raw/ folder.

    Step --> 2. Use this method and replace .cer file name with your .cer file and replace your host name also.

    private SSLSocketFactory getSocketFactory() {
    
        CertificateFactory cf = null;
        try {
    
            cf = CertificateFactory.getInstance("X.509");
            InputStream caInput = getResources().openRawResource(R.raw.cert_name);
            Certificate ca;
            try {
    
                ca = cf.generateCertificate(caInput);
                Log.e("CERT", "ca=" + ((X509Certificate) ca).getSubjectDN());
            } finally {
                caInput.close();
            }
    
    
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);
    
    
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
            tmf.init(keyStore);
    
    
            HostnameVerifier hostnameVerifier = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
    
                    Log.e("CipherUsed", session.getCipherSuite());
                    return hostname.compareTo("10.199.89.68")==0; //The Hostname of your server.
    
                }
            };
    
    
            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            SSLContext context = null;
            context = SSLContext.getInstance("TLS");
    
            context.init(null, tmf.getTrustManagers(), null);
            HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    
            SSLSocketFactory sf = context.getSocketFactory();
    
    
            return sf;
    
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
    
        return  null;
    }
    

    Step --> 3. Replace this line "RequestQueue queue = Volley.newRequestQueue(this);" with "RequestQueue queue = Volley.newRequestQueue(this, new HurlStack(null, getSocketFactory()));" in request of volley.

提交回复
热议问题