Retrofit 제품군의 HTTPS는 모든 인증서를 신뢰합니다.

3467 단어
// 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();
                    }
                } );

좋은 웹페이지 즐겨찾기