HttpClient和OkHttp关于Https请求问题

来源:互联网 发布:中国物联网域名注册 编辑:程序博客网 时间:2024/06/05 16:20

项目中遇到登录接口,使用的时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());    }
0 0