Retrofit系列之HTTPS信任所有证书

//首先创建OkHttpClient.build进行信任所有证书配置
OkHttpClient.Builder okhttpClient = new OkHttpClient().newBuilder();
                //信任所有服务器地址
                okhttpClient.hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String s, SSLSession sslSession) {
                        //设置为true
                        return true;
                    }
                });
                //创建管理器
                TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(
                            java.security.cert.X509Certificate[] x509Certificates,
                            String s) throws java.security.cert.CertificateException {
                    }

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

                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[] {};
                    }
                } };
                try {
                    SSLContext sslContext = SSLContext.getInstance("TLS");
                    sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

                    //为OkHttpClient设置sslSocketFactory
                    okhttpClient.sslSocketFactory(sc.getSocketFactory());

                } catch (Exception e) {
                    e.printStackTrace();
                }


                //创建Retrofit
                Retrofit httpsRetrofit = new Retrofit.Builder().baseUrl( baseLogin )
                                                              .addConverterFactory( GsonConverterFactory.create() )
                                                               //设置配置好的okHttpClient
                                                               .client( okhttpClient.build() )
                                                              .build();
                //创建动态代理
                GetAndPostService httpsService = httpsRetrofit.create( GetAndPostService.class );
                //进行post请求
                Map httpsMap = new HashMap<>(  );
                httpsMap.put( "username", "username" );
                httpsMap.put( "password", "password" );
                Call httpsCall = httpsService.httpsData(httpsMap);
                httpsCall.enqueue( new Callback< CrmLoginBean >() {
                    @Override
                    public void onResponse( Call< CrmLoginBean > call, Response< CrmLoginBean > response ) {

                        Toast.makeText(LoginActivity.this, "message="+response.message()+",code="+response.code()+",response="+response.body().toString(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure( Call< CrmLoginBean > call, Throwable t ) {
                        Toast.makeText(LoginActivity.this, t.getMessage()+","+t.toString()+","+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                    }
                } );

你可能感兴趣的:(Retrofit系列之HTTPS信任所有证书)