HttpClient对于https的支持和配置

这篇博客针对Android新手,详细介绍了如何配置HttpClient以支持https。提供了可以直接使用的代码示例,旨在帮助开发者快速实现HTTPS连接功能。
摘要由CSDN通过智能技术生成

网上有许多对于https的支持配置,但是对于一些新手而言,显然不够直接拿来复制粘贴然后运行出来,所以今天就将整套代码拿出来与大家分享,希望指正错误和帮我优化代码


public static DefaultHttpClient create(boolean isHttps) throws Exception{
        HttpParams params = createHttpParams();
        DefaultHttpClient httpClient = null;
        if (isHttps) {
            // 支持httphttps
            KeyStore trustStore = null;
            try {
                trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                trustStore.load(null, null);
                SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
//            schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
                schemeRegistry.register(new Scheme("https", sf, 443));
            // ThreadSafeClientConnManager线程安全管理类
            ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
            httpClient = new DefaultHttpClient(cm, params);
            } catch (Exception e) {
            }
        } else {
            httpClient = new DefaultHttpClient(params);
        }
        return httpClient;
    }
 上述代码很明显是获取httpClient的实体,分为支持或者不支持https俩种,简单明了 

private static class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("TLS");

    public MySSLSocketFactory(KeyStore truststore)
            throws NoSuchAlgorithmException, KeyManagementException,
            KeyStoreException, UnrecoverableKeyException {
        super(truststore);

        TrustManager tm = new X509TrustManager() {
            public void checkClientTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            public void checkServerTrusted(X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        };

        sslContext.init(null, new TrustManager[]{tm}, null);
    }

    @Override
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
            throws IOException, UnknownHostException {
        return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
    }

    @Override
    public Socket createSocket() throws IOException {
        return sslContext.getSocketFactory().createSocket();
    }
}

private static HttpParams createHttpParams() {
    final HttpParams params = new BasicHttpParams();
    // 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值