排球和https请求

时间:2018-06-19 13:44:32

标签: android https

我尝试通过截击执行https请求。

 JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                urlHttps, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                .....


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

            }
        }) {

            @Override
            public byte[] getBody() {
                try {
                    return requestBody == null ? null : requestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                            requestBody, "utf-8");
                    return null;
                }
            }
        };

        int MY_SOCKET_TIMEOUT_MS = 60000;

        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
                MY_SOCKET_TIMEOUT_MS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(jsonObjectRequest);

我收到此消息

javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.

因此,我创建了一个带有自动签名证书的bks文件,并尝试使用它来执行受信任的请求?

RequestQueue requestQueue = Volley.newRequestQueue(this, new HurlStack(null, getSSLSocketFactoryBks()));




    private SSLSocketFactory getSSLSocketFactory() {


            try {
                KeyStore keyStoreTrusted = KeyStore.getInstance("BKS");
                String password = "mysecret";
                String strFilePathBks = application.getDataBaseDirectory() + File.separator + getResources().getString(R.string.keystore_file_name);

                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(strFilePathBks);
                    InputStream is = fis;
                    keyStoreTrusted.load(fis, password.toCharArray());

                } catch (Exception ex) {
                    GILogUtil.e(TAG, "SSLSocketFactory - Erreur lecture bks");
                } finally {
                    if (fis != null) fis.close();
                }

                String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
                TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
                tmf.init(keyStoreTrusted);

                HostnameVerifier hostnameVerifier = new HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, SSLSession session) {
                        return true;
                    }
                };

                HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

                TrustManager[] wrappedTrustManagers = getWrappedTrustManagers(tmf
                        .getTrustManagers());
                SSLContext context = SSLContext.getInstance("TLS");
                context.init(null, wrappedTrustManagers, null);

                SSLSocketFactory sf = context.getSocketFactory();

                return sf;
            } catch (Exception e) {
                throw new AssertionError(e);
            }
        }

 private TrustManager[] getWrappedTrustManagers(TrustManager[] trustManagers) {
        final X509TrustManager originalTrustManager = (X509TrustManager) trustManagers[0];
        return new TrustManager[] { new X509TrustManager() {
            public X509Certificate[] getAcceptedIssuers() {
                return originalTrustManager.getAcceptedIssuers();
            }

            public void checkClientTrusted(X509Certificate[] certs,
                                           String authType) {
               try {

                   if (certs != null && certs.length > 0) {
                        certs[0].checkValidity();
                    } else {
                        originalTrustManager
                                .checkClientTrusted(certs, authType);
                    }
                } catch (CertificateException e) {
                    GILogUtil.e("checkClientTrusted", e.toString());
                }
            }

            public void checkServerTrusted(X509Certificate[] certs,
                                           String authType) {
               try {

                   GILogUtil.i(TAG, "checkServerTrusted: nb certs :" + certs.length);
                   if (certs != null && certs.length > 0) {
                        certs[0].checkValidity();
                    } else {
                        originalTrustManager
                                .checkServerTrusted(certs, authType);
                    }
                } catch (CertificateException e) {

                }
            }
        } };
    }

所以我有此错误,这不是证书bks,但这是服务器证书错误 为什么呢如何使用我的证书?我不明白吗?有什么解决方案?

checkServerTrusted: java.security.cert.CertificateExpiredException: Certificate expired at Mon May 29 14:27:29 GMT+00:00 2017 (compared to Tue Jun 19 13:41:19 GMT+00:00 2018)
 Error response approval : javax.net.ssl.SSLPeerUnverifiedException: Hostname 150.150.99.206 not verified:

0 个答案:

没有答案