Hostname in certificate didn't match?

前端 未结 5 1105
挽巷
挽巷 2020-12-13 11:37

I try to connect to a server with a self-signed certificate. I use this code to accept all certificates.

public class CertificateAcceptor {

    public void          


        
5条回答
  •  春和景丽
    2020-12-13 11:53

    The Answer by Prashant may not work, as you need to initialize the SSLContext as well.

    I would do it something like,

    SSLSocketFactory sf=null ;
            SSLContext sslContext = null;
            StringWriter writer;
            try {
                sslContext = SSLContext.getInstance("TLS")  ;
                sslContext.init(null,null,null);
            } catch (NoSuchAlgorithmException e) {
                //
            }  catch (KeyManagementException e){
                //
            }
    
            try{
                sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    
            } catch(Exception e) {
                //
    
        }
            Scheme scheme = new Scheme("https",443,sf);
            httpClient.getConnectionManager().getSchemeRegistry().register(scheme);
    

提交回复
热议问题