xUtils-2.6.14在android5.0以下系统https问题

首先自定义 SSLSocketFactory 

package com.lidroid.xutils.util;


import android.os.Build;


import com.lidroid.xutils.http.client.DefaultSSLSocketFactory;


import org.apache.http.conn.ssl.SSLSocketFactory;


import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;


import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


/**
 * Created by  on 2017/10/25 14:22.
 * 邮箱:sunxiangyuan12@163.com
 */
public    class SSLSocketFactoryEx extends SSLSocketFactory {


    static String protocols[] = null, cipherSuites[] = null;


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


    static {
        try {
            SSLSocket socket = (SSLSocket) DefaultSSLSocketFactory.getSocketFactory().createSocket();
            if (socket != null) {
                /* set reasonable protocol versions */
                // - enable all supported protocols (enables TLSv1.1 and TLSv1.2 on Android <5.0)
                // - remove all SSL versions (especially SSLv3) because they're insecure now
                List<String> protocols = new LinkedList<>();
                for (String protocol : socket.getSupportedProtocols())
                    if (!protocol.toUpperCase().contains("SSL"))
                        protocols.add(protocol);
                SSLSocketFactoryEx.protocols = protocols.toArray(new String[protocols.size()]);
                /* set up reasonable cipher suites */
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    // choose known secure cipher suites
                    List<String> allowedCiphers = Arrays.asList(
                            // TLS 1.2
                            "TLS_RSA_WITH_AES_256_GCM_SHA384",
                            "TLS_RSA_WITH_AES_128_GCM_SHA256",
                            "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
                            "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
                            "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
                            "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",
                            "TLS_ECHDE_RSA_WITH_AES_128_GCM_SHA256",
                            // maximum interoperability
                            "TLS_RSA_WITH_3DES_EDE_CBC_SHA",
                            "TLS_RSA_WITH_AES_128_CBC_SHA",
                            // additionally
                            "TLS_RSA_WITH_AES_256_CBC_SHA",
                            "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",
                            "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
                            "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
                            "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA");
                    List<String> availableCiphers = Arrays.asList(socket.getSupportedCipherSuites());
                    // take all allowed ciphers that are available and put them into preferredCiphers
                    HashSet<String> preferredCiphers = new HashSet<>(allowedCiphers);
                    preferredCiphers.retainAll(availableCiphers);
                    /* For maximum security, preferredCiphers should *replace* enabled ciphers (thus disabling
                     * ciphers which are enabled by default, but have become unsecure), but I guess for
                     * the security level of DAVdroid and maximum compatibility, disabling of insecure
                     * ciphers should be a server-side task */
                    // add preferred ciphers to enabled ciphers
                    HashSet<String> enabledCiphers = preferredCiphers;
                    enabledCiphers.addAll(new HashSet<>(Arrays.asList(socket.getEnabledCipherSuites())));
                    SSLSocketFactoryEx.cipherSuites = enabledCiphers.toArray(new String[enabledCiphers.size()]);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    public SSLSocketFactoryEx(KeyStore truststore)


            throws NoSuchAlgorithmException, KeyManagementException,


            KeyStoreException, UnrecoverableKeyException {


        super(truststore);






        TrustManager tm = new X509TrustManager() {






            @Override


            public java.security.cert.X509Certificate[] getAcceptedIssuers() {


                return null;


            }






            @Override


            public void checkClientTrusted(


                    java.security.cert.X509Certificate[] chain, String authType)


                    throws java.security.cert.CertificateException {






            }






            @Override


            public void checkServerTrusted(


                    java.security.cert.X509Certificate[] chain, String authType)


                    throws java.security.cert.CertificateException {






            }


        };






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


    }






    @Override


    public Socket createSocket(Socket socket, String host, int port,


                               boolean autoClose) throws IOException, UnknownHostException {


        Socket ssl = sslContext.getSocketFactory().createSocket(socket, host, port,


                autoClose);
        if (ssl instanceof SSLSocket)
            upgradeTLS((SSLSocket)ssl);
        return ssl;


    }






    @Override


    public Socket createSocket() throws IOException {
        Socket ssl = sslContext.getSocketFactory().createSocket();
        if (ssl instanceof SSLSocket)
            upgradeTLS((SSLSocket)ssl);
        return ssl;


    }
    private void upgradeTLS(SSLSocket ssl) {
        // Android 5.0+ (API level21) provides reasonable default settings
        // but it still allows SSLv3
        // https://developer.android.com/about/versions/android-5.0-changes.html#ssl
        if (protocols != null) {
            ssl.setEnabledProtocols(protocols);
        }
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP && cipherSuites != null) {
            ssl.setEnabledCipherSuites(cipherSuites);
        }
    }

然后:xutil初始化时添加如下代码

 public static final void init(Context context) {
        if (httpUtils == null) {
            synchronized (MyHttpUtil.class) {
                if(httpUtils == null){
                    cookieStore = new PreferencesCookieStore(context);
// cookieStore = new BasicCookieStore();
                    httpUtils = new HttpUtils();
                    httpUtils.configTimeout(50 * 1000);
                    httpUtils.configHttpRedirectHandler(new HttpUtilDefaultHttpRedirectHandler());
                    httpUtils.configCurrentHttpCacheExpiry(0);
                    httpUtils.configCookieStore(cookieStore);
                    if(Build.VERSION.SDK_INT <Build.VERSION_CODES.LOLLIPOP){
                        try {
                            KeyStore trustStore = KeyStore.getInstance(KeyStore
                                    .getDefaultType());
                            trustStore.load(null, null);
                            SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
//                sf.
                            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  //允许所有主机的验证
                            httpUtils.getHttpClient().getConnectionManager().getSchemeRegistry().unregister("https");
                            httpUtils.configRegisterScheme(new Scheme("https", sf, 443));
//                schReg.register(new Scheme("http", PlainSocketFactory
//
//                                .getSocketFactory(), 80));
//
//                schReg.register(new Scheme("https", sf, 443));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
//            final SSLSocketFactory sslSocketFactory = DefaultSSLSocketFactory.getSocketFactory();
//            sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
//            utils.configSSLSocketFactory(sslSocketFactory);
                    }
                }
            }
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个Android基于快速开发的一个框架 xUtils 它是在aFinal基础上进行重构和扩展的框架 相比aFinal有很大的改善 同时 如果如果你的应用是基于网络的 那么只要处理得当 它会让你彻底的摆脱各种工具类和重复代码的困扰 xUtils 包含了很多实用的android工具 xUtils 源于Afinal框架 对Afinal进行了大量重构 使得xUtils支持大文件上传 更全面的http请求协议支持 拥有更加灵活的ORM 更多的事件注解支持且不受混淆影响 xUtils的四大组件: 一 ViewUtils 你受够了重复冗长的findViewById了嘛 你受够了各种监听事件的绑定了嘛 在这里 你只需要一句注解 如@ViewInject @OnClick 就能轻松摆脱小白似的代码 大大的上了一个档次 二 HttpUtils 支持的HTTP七种请求方式 非常便捷的满足你的接口请求的需要 同时还支持大文件上传下载 以及同步异步请求 三 BitmapUtils 你的程序因OOM强制关闭过嘛 你在为加在网络图片头疼嘛 有了组件 你将永久摆脱前面的问题 四 DbUtils 简单易用又出色的ORM框架 真的是谁用谁知道 直接轻松存储各种对象到sqlite数据库中 同时也能非常方便的进行各种条件查询 甚至分页查询 还有对表中数据的更新删除等操作 真正的实现 一行代码就可以进行增删改查 并且可通过注解自定义表名 列名 外键 唯一性约束 NOT NULL约束 CHECK约束等 支持事务 摘自github ">一个Android基于快速开发的一个框架 xUtils 它是在aFinal基础上进行重构和扩展的框架 相比aFinal有很大的改善 同时 如果如果你的应用是基于网络的 那么只要处理得当 它会让你彻底的摆脱各种工具类和重复代码的困扰 [更多]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值