Exception : javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

前端 未结 7 2366
时光取名叫无心
时光取名叫无心 2020-12-08 06:06
public HttpClientVM() {

    BasicHttpParams params = new BasicHttpParams();
    ConnManagerParams.setMaxTotalConnections(params, 10);
    HttpProtocolParams.setVers         


        
7条回答
  •  情书的邮戳
    2020-12-08 06:54

    This error is because your server doesn't have a valid SSL certificate. Hence we need to tell the client to use a different TrustManager. Here is a sample code:

    SSLContext ctx = SSLContext.getInstance("TLS");
    X509TrustManager tm = new X509TrustManager() {
    
        public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        }
    
        public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
        }
    
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    ctx.init(null, new TrustManager[]{tm}, null);
    SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    ClientConnectionManager ccm = base.getConnectionManager();
    SchemeRegistry sr = ccm.getSchemeRegistry();
    sr.register(new Scheme("https", 443, ssf));
    
    client = new DefaultHttpClient(ccm, base.getParams());
    

提交回复
热议问题