HttpClient和OkHttp关于Https请求问题

项目中遇到登录接口,使用的时https接口,所以客户端必须要进行配置,原先的项目使用的时Apache的Httpclient,Android的api接口也内置了httpclient,但是Android系统在4.x以后不建议使用了Httpclient,推荐使用urlconnection,但是urlconnection毕竟太过于原始,所以我选择了okhttp作为http请求库。

OKhttp的地址

由于服务器使用的是通用的https加密,所以我在这里只是介绍通用的加密方式。

Httpclient

    static class SSLSocketFactoryEx extends SSLSocketFactory {

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

        public SSLSocketFactoryEx(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();
        }
    }



    static HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);

            SSLSocketFactoryEx sf = new SSLSocketFactoryEx(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));

            ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

OkHttp

    static class HttpStackPlus extends HurlStack {
        private final OkUrlFactory mFactory;

        public HttpStackPlus() {
            this(new OkHttpClient());
        }

        public HttpStackPlus(OkHttpClient client) {
            if (client == null) {
                throw new NullPointerException("Client must not be null.");
            } else {
                try {
                    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 e = SSLContext.getInstance("TLS");

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

                    client.setSslSocketFactory(e.getSocketFactory());

                } catch (Exception e) {
                    throw new AssertionError();
                }

                this.mFactory = new OkUrlFactory(client);
            }
        }

        protected HttpURLConnection createConnection(URL url) throws IOException {
            return this.mFactory.open(url);
        }
    }

    public static RequestQueue newRequestQueue(Context context) {
        return Volley.newRequestQueue(context, new HttpStackPlus());
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值