httpclient忽略证书验证允许https请求

public static void enableSSL(DefaultHttpClient httpclient) {

    // 调用ssl

    try {

        SSLContext sslcontext = SSLContext.getInstance("TLS");

        sslcontext.init(null, new TrustManager[]{truseAllManager}, null);

        @SuppressWarnings("deprecation")

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext);

        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        Scheme https = new Scheme("https", sf, 443);

        httpclient.getConnectionManager().getSchemeRegistry()

                .register(https);

    } catch (Exception e) {

        e.printStackTrace();

    }

}


使用方式:

DefaultHttpClient httpclient = new DefaultHttpClient();

enableSSL(httpclient);


你可能感兴趣的:(Java技术)